# Function calling
course: Academy — 54-Next-Gen-AI-GenAI-Agents-Future-Trends
module: Academy/54-Next-Gen-AI-GenAI-Agents-Future-Trends
type: notebook
source_url: https://personal-learn.armco.dev/files/Academy/54-Next-Gen-AI-GenAI-Agents-Future-Trends/folders/Session_8_Folder/Function_calling.ipynb
---
[cell 1 code]
# pip install openai
import openai
import pandas as pd
import os
from dotenv import load_dotenv
[cell 2 code]
apikey = os.getenv('OPENAI_API_KEY')
from openai import OpenAI
client = OpenAI(api_key = apikey)
[cell 3 code]
completion=client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a assistant which informs about temperature."},
{"role": "user", "content": "Hey there"}
]
)
[cell 4 code]
import requests
def get_current_weather(location):
url = "https://ai-weather-by-meteosource.p.rapidapi.com/find_places"
querystring = {"text":location,"language":"en"}
headers = {
"x-rapidapi-key": "2eb5622020msh67823b08b1d30f3p10fa70jsn354e098636e5",
"x-rapidapi-host": "ai-weather-by-meteosource.p.rapidapi.com"
}
response = requests.get(url, headers=headers, params=querystring)
return response.json()
get_current_weather("Bengaluru")
[cell 5 code]
import requests
def get_current_weather(location):
"""Get the current weather in a given location"""
url = "https://ai-weather-by-meteosource.p.rapidapi.com/find_places"
querystring = {"text":location}
headers = {
"x-rapidapi-key": "2eb5622020msh67823b08b1d30f3p10fa70jsn354e098636e5",
"x-rapidapi-host": "ai-weather-by-meteosource.p.rapidapi.com"
}
response = requests.get(url, headers=headers, params=querystring)
print(response.json())
return response.json()
[cell 6 code]
functions = [
{
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
},
"required": ["location"],
},
}
]
[cell 7 code]
functions
[cell 8 code]
user_message="Hi There"
messages=[]
messages.append({"role": "user", "content":user_message})
completion=client.chat.completions.create(
model="gpt-3.5-turbo",
messages=messages
)
[cell 9 code]
print(completion.choices[0].message)
[cell 10 code]
user_message="What is the temperature of Bangalore"
messages.append({"role": "user", "content": user_message})
completion=client.chat.completions.create(
model="gpt-3.5-turbo",
messages=messages,
functions=functions
)
[cell 11 code]
print(completion.choices[0].message)
[cell 12 code]
messages
[cell 13 code]
response = completion.choices[0].message
function_name=response.function_call.name
print(function_name)
[cell 14 code]
import json
jsonstr = response.function_call.arguments
data = json.loads(jsonstr)
location = data['location']
location
[cell 15 code]
messages.append(response)
[cell 16 code]
# messages.append(response)
messages.append(
{
"role": "function",
"name": function_name,
"content": location,
}
)
[cell 17 code]
messages
[cell 19 code]
# extend conversation with function response
second_response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=messages,
functions=functions
) # get a new response from GPT where it can see the function response
[cell 20 code]
print(second_response.choices[0].message)