import os
from openai import OpenAI
os.environ[‘OPENAI_API_KEY’]=‘’
from openai import OpenAI
import json
import requests
client = OpenAI()
def reading_stocks_api(company):
url = "https://real-time-finance-data.p.rapidapi.com/search"
querystring = {"query":company,"language":"en"}
headers = {
"X-RapidAPI-Key": "3879ab75d6msh78eedf538a6ae35p1e135djsnf399e2f53995",
"X-RapidAPI-Host": "real-time-finance-data.p.rapidapi.com"
}
response = requests.get(url, headers=headers, params=querystring)
return response.json()
#reading_stocks_api(‘airtel’)
messages=[
{“role”: “user”, “content”: " what is the present stock price of airtel "}
]
client = OpenAI()
completion = client.chat.completions.create(
model=“gpt-3.5-turbo-0125”,
messages=messages)
messages.append(completion.choices[0].message)
tools = [
{
“type”: “function”,
“function”: {
“name”: “reading_stocks_api”,
“description”: “Get the current stock price for the given company name in the query”,
“parameters”: {
“type”: “object”,
“properties”: {
“company”: {
“type”: “string”,
“description”: “get the company name like vodafone ,apple”
},
},
“required”: [“company”]
}
}
}
]
client = OpenAI()
completion = client.chat.completions.create(
model=“gpt-3.5-turbo”,
messages=messages,
tools=tools)
messages.append(completion.choices[0].message)
content=json.loads(completion.choices[0].message.tool_calls[0].function.arguments)
content[‘company’]
messages.append(
{
“tool_call_id”: completion.choices[0].message.tool_calls[0].id,
“role”: “tool”,
“name”: completion.choices[0].message.tool_calls[0].function.name,
“content”: content[‘company’],
}
)
second_response = client.chat.completions.create(
model=“gpt-3.5-turbo-0125”,
messages=messages,
)
second_response.choices[0].message.content
my first input :- what is the present stock price of airtel
the output form model
I’m sorry, but I am unable to provide real-time stock prices. I recommend checking a financial news website or a stock market app for the most up-to-date information on the stock price of Airtel."
after using function calling output is :- I am currently unable to retrieve the stock price of Airtel. I recommend checking a financial news website or using a stock market app for the most up-to-date information.
what is wrong in my code ?? any help ??