> For the complete documentation index, see [llms.txt](https://teamsmiley.gitbook.io/devops/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://teamsmiley.gitbook.io/devops/ai/langchain/function-call.md).

# function-call

오해가 많음..

openai에서 결과를 받는것이 아님

기존 질문 : "{country}의 수도가 어디야?" 라고 물으면 서울이라는 답변이 오는데

function calling을 하면 "나는 한국의 수도가 어디인지 알고싶어?" 라고 물으면

get\_city(country) 라는 함수가 잇다고하면 "함수 get\_city를 실행하고 argument 로 한국을 넣어라." 라고 알려주는것. 실제로 함수를 실행하는것도 아님.

그러니 실제로 openai에서 결과를 받는것이 아님

결과는 get\_city에서 처리해야함.

```python
from dotenv import load_dotenv
load_dotenv()
```

```python
from langchain.prompts import PromptTemplate
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
  temperature=0.1,
)

```

```python
template = "{country}의 수도는 뭐야?"


# 템플릿 완성
prompt = PromptTemplate.from_template(template=template)
prompt

```

```python
chain = prompt | llm
```

```python

chain.invoke({"country":"한국"})
```

add function

```python
def get_city(country):
    print(country)
```

add schema

```python
schema = {
  "name": "get_city",
  "description": "나라의 수도를 가져옵니다.",
  "parameters": {
    "type": "object",
    "properties": {
      "country": { "type": "string","description": "나라 이름" }
    }
  },
  "required": ["country"],
}
```

```python
llm = ChatOpenAI(
    temperature=0.1,
).bind(
  function_call={
      "name": "get_city",
  },
  functions=[
      schema,
  ],
)
```

```python
template = "{country}의 수도는 뭐야?"


# 템플릿 완성
prompt = PromptTemplate.from_template(template=template)
prompt
chain = prompt | llm
chain.invoke({"country": "한국"})

```

```json
AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{"country":"한국"}', 'name': 'get_city'}}, )
```

```python
response = chain.invoke({"country": "한국"})

```

```python
arg_from_ai = response.additional_kwargs["function_call"]
arg_from_ai
```

```python
arg_from_ai["arguments"]
arg_from_ai["name"]
```

```python
import json

arguments = json.loads(arg_from_ai["arguments"])
country = arguments["country"]
country
```

```python
if arg_from_ai["name"] == "get_city":
    get_city(country)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://teamsmiley.gitbook.io/devops/ai/langchain/function-call.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
