OpenAI Perform Calling
OpenAI just lately launched a brand new characteristic known as “function calling“, which permits builders to create extra interactive and dynamic purposes. With the operate calling characteristic, builders can describe capabilities to OpenAI’s mannequin, and the mannequin will output a JSON object containing arguments to name these capabilities. Perform calling permits builders to get structured knowledge again from the mannequin. Some widespread use circumstances of the “Perform Calling” characteristic are as beneath:
- To name the exterior APIs.
- To transform pure language queries to API calls or database calls
- Extract structured knowledge from textual content
On this weblog publish, we’ll create a small software that makes use of OpenAI’s function-calling characteristic to name exterior APIs. These APIs will present us with the present inventory worth of an organization listed in america and the present foreign money alternate fee between the 2 nations.
To get the present inventory worth of an organization listed in america, we’ll use Finnhub. Finnhub supplies a free API key that may be obtained by following the beneath steps:
It can offer you the API key, which we’ll use to fetch inventory costs.
To get the present foreign money alternate fee between the 2 nations, we’ll use Alphavantage. Alpha Vantage additionally supplies a free API key that may be obtained by following the beneath steps:
It can offer you the API key, which we’ll use to fetch foreign money alternate charges between 2 nations.
Now that we’ve got each the API keys of Finnhub and Alpha Vantage, we are able to begin constructing the appliance. Beneath is a step-by-step information you may comply with to create an software:
Step 1:
To start, it’s worthwhile to set up two packages, openai, and finnhub-python, by utilizing the next instructions:
pip set up openai
pip set up finnhub-python
As soon as the set up is full, you may import the mandatory libraries by together with the next strains in your code:
import json
import requests
import finnhub
Step 2:
First, we’re making a utility that facilitates requests to the Chat Completions API. The utility operate takes 4 parameters: messages, capabilities, function_call, and mannequin. The aim of every parameter is as follows:
messages: It takes a immediate that might be used when making the ChatGPT API name.
capabilities: It can take the record of accessible capabilities specification, which will be invoked by ChatGPT.
function_call: This parameter permits us to offer both the identify of a particular operate or set it as “None”. When a operate identify is supplied, the API might be directed to name that exact operate. If we wish to drive the OpenAI mannequin to not use any operate, then we have to cross this argument because the None.
mannequin: t will take the identify of the mannequin we wish to use, right here we’re utilizing the “gpt-3.5-turbo-0613” mannequin. You should utilize “gpt-4-0613” additionally.
This operate will name the OpenAI Chat Completion endpoint with acceptable parameters and return the response.
GPT_MODEL = "gpt-3.5-turbo-0613"
def chat_completion_request(messages, capabilities=None, function_call=None, mannequin=GPT_MODEL):
headers = {
"Content material-Kind": "software/json",
"Authorization": "Bearer " + "<your_openai_key>",
}
json_data = {"mannequin": mannequin, "messages": messages}
if capabilities isn't None:
json_data.replace({"capabilities": capabilities})
if function_call isn't None:
json_data.replace({"function_call": function_call})
attempt:
response = requests.publish(
"https://api.openai.com/v1/chat/completions",
headers=headers,
json=json_data,
)
return response
besides Exception as e:
print("Unable to generate ChatCompletion response")
print(f"Exception: {e}")
return e
Step 3:
Subsequent, we’ll develop a operate for invoking the Finnhub API to retrieve the present inventory worth of an organization listed in america. First, it’s worthwhile to create a finnhub shopper by passing your finnhub API. Then we are able to write our operate like beneath:
finnhub_client = finnhub.Consumer(api_key="<Your_finnhub_API_key>")
def get_current_stock_price(arguments):
attempt:
arguments = json.hundreds(arguments)['ticker_symbol']
price_data=finnhub_client.quote(arguments)
stock_price = price_data.get('c', None)
if stock_price == 0:
return "This firm isn't listed inside USA, please present one other identify."
else:
return stock_price
besides:
return "This firm isn't listed inside USA, please present one other identify."
Step 4:
Subsequent, we’ll create a operate that can name Alpha Vantage API to fetch the foreign money alternate fee between the two nations. You could add the beneath strains of code to implement the operate.
def currency_exchange_rate(arguments):
attempt:
from_country_currency = json.hundreds(arguments)['from_country_currency']
to_country_currency = json.hundreds(arguments)['to_country_currency']
url = f'https://www.alphavantage.co/question?operate=CURRENCY_EXCHANGE_RATE&from_currency={from_country_currency}&to_currency={to_country_currency}&apikey='<your_Alphavantage_api_key>''
r = requests.get(url)
knowledge = r.json()
return knowledge['Realtime Currency Exchange Rate']['5. Exchange Rate']
besides:
return "I'm unable to parse this, please attempt one thing new."
Step 5:
Now, we have to create operate specs for interacting with the Finnhub API and Alpha Vantage API. We’ll cross these operate specs to the Chat Completions API to be able to generate operate arguments that adhere to the specification. On this operate specification, it’s worthwhile to present the identify and outline of the operate and its parameters. You possibly can describe the practical specification as beneath:
capabilities = [
{
"name": "get_current_stock_price",
"description": "It will get the current stock price of the US company.",
"parameters": {
"type": "object",
"properties": {
"ticker_symbol": {
"type": "string",
"description": "This is the symbol of the company.",
}
},
"required": ["ticker_symbol"],
},
},
{
"identify": "currency_exchange_rate",
"description": "It can get the foreign money alternate fee between 2 nations.",
"parameters": {
"kind": "object",
"properties": {
"from_country_currency": {
"kind": "string",
"description": "That is the foreign money of the nation whose we have to map.",
},
"to_country_currency": {
"kind": "string",
"description": "That is the foreign money of the nation to which we have to map.",
}
},
"required": ["from_country_currency","to_country_currency"],
},
}]
Step 6:
Now, we are able to make the most of the function-calling functionality of OpenAI. We have to name chat completion API with immediate and performance specs. It can name ChatGPT and generate JSON that we are able to use to name the operate in our code. You should utilize the beneath code to generate JSON which might be used additional to name an area operate.
user_input = enter("Please enter your query right here: ")
# immediate
messages = [{"role": "system", "content": "Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous."}]
messages.append({"position": "consumer", "content material": user_input})
# calling chat_completion_request to name ChatGPT completion endpoint
chat_response = chat_completion_request(
messages, capabilities=capabilities
)
# fetch response of ChatGPT and name the operate
assistant_message = chat_response.json()["choices"][0]["message"]
print(assistant_message)
Output:
We requested, “What’s the inventory worth of Apple?”, ChatGPT acknowledged the necessity to invoke the ‘get_current_stock_price’ operate with the parameter ‘AAPL’ to be able to entry the Finnhub API. So, it produced a JSON response containing the operate identify and the corresponding parameter required to execute the native operate.
Please enter your query right here: What's the inventory worth of Apple?
{'position': 'assistant', 'content material': None, 'function_call': {'identify': 'get_current_stock_price', 'arguments': '{n "ticker_symbol": "AAPL"n}'}}
Step 7:
Now, we are able to name the native operate ‘get_current_stock_price’, utilizing the JSON generated by ChatGPT as beneath:
if assistant_message['content']:
print("Response is: ", assistant_message['content'])
else:
fn_name = assistant_message["function_call"]["name"]
arguments = assistant_message["function_call"]["arguments"]
operate = locals()[fn_name]
outcome = operate(arguments)
print("Response is: ", outcome)
Output:
Please enter your query right here: What's the inventory worth of Apple?
{'position': 'assistant', 'content material': None, 'function_call': {'identify': 'get_current_stock_price', 'arguments': '{n "ticker_symbol": "AAPL"n}'}}
Response is: 184.92
Step 8:
Now, we are able to merge the above 2 steps, to generate JSON utilizing ChatGPT and name an area operate utilizing generated JSON and put them into the loop like beneath:
user_input = enter("Please enter your query right here: (if you wish to exit then write 'exit' or 'bye'.) ")
whereas user_input.strip().decrease() != "exit" and user_input.strip().decrease() != "bye":
# immediate
messages = [{"role": "system", "content": "Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous."}]
messages.append({"position": "consumer", "content material": user_input})
# calling chat_completion_request to name ChatGPT completion endpoint
chat_response = chat_completion_request(
messages, capabilities=capabilities
)
# fetch response of ChatGPT and name the operate
assistant_message = chat_response.json()["choices"][0]["message"]
if assistant_message['content']:
print("Response is: ", assistant_message['content'])
else:
fn_name = assistant_message["function_call"]["name"]
arguments = assistant_message["function_call"]["arguments"]
operate = locals()[fn_name]
outcome = operate(arguments)
print("Response is: ", outcome)
user_input = enter("Please enter your query right here: ")
Outcomes:
Please enter your query right here: (if you wish to exit then write 'exit' or 'bye'.) 1 greenback is the same as what rs?
Response is: 81.94000000
Please enter your query right here: What's the present inventory worth of Amazon?
Response is: 125.4902
Please enter your query right here: Forex alternate fee between india and canada?
Response is: 0.01606000
Please enter your query right here: Inventory worth of Tesla?
Response is: 260.54
You can even use the beneath colab to run the above code.
https://colab.research.google.com/drive/1Ki1gDUfF8bWw8Z-pr0IDHtLrGXM2G7NL?usp=sharing