This is my code:
# import logging
# import time
import openai
from dotenv import find_dotenv, load_dotenv
import requests
import os
import json
# import streamlit as st
load_dotenv()
client = openai.OpenAI()
model = os.getenv("MODEL")
temperature = float(os.getenv("TEMPERATURE"))
bearer_token = os.getenv("BEARER_TOKEN")
#ibyW5IZ6nmvaw7nIMMTMz
conversation_history = []
functions = [
{
"name": "get_customer",
"type": "function",
"function": {
"name": "get_customer",
"description": "Get a customer's details based on their customer ID",
"parameters": {
"type": "object",
"properties": {
"customerID": {
"type": "string",
"description": "ID of the customer"
}
},
"required": ["customerID"]
}
}
}
]
def get_customer(customerID):
url = (
f"https://localhost/usage-engine/customer/{customerID}"
)
try:
response = requests.get(url, headers={"Authorization": "Bearer " + bearer_token})
if response.status_code == 200:
offers = json.dumps(response.json(), indent=4)
customer_json = json.loads(offers)
data = customer_json
print(data)
customerId = data['customerInfo']['customerId']
createdAt = data['customerInfo']['createdAt']
customer_details = f"""
Customer ID: {customerId},
Created At: {createdAt},
"""
return customer_details
else:
return []
except requests.exceptions.RequestException as e:
print("Error occured during API request", e)
while True:
user_input = input("Enter your message: ")
if user_input!=None:
conversation_history.append({"role": "user", "content": user_input})
conversation_history.append({"role": "system", "content": "Don't call 'get_customer' unnessarily. Only call function 'get_customer' if all arguments are given. ask the user for the parameters if it is not present, before calling. never ever ever call the function without mandatory arguments and parameters"})
response = client.chat.completions.create(
model=model,
messages=conversation_history,
temperature=temperature,
functions=functions,
function_call="auto"
)
if response.choices[0].message.content!=None:
conversation_history.append({"role": "assistant", "content": response.choices[0].message.content})
# #print("Bot:",response.choices[0].message.content)
response_message = response.choices[0].message
print("Bot:",response_message)
if dict(response_message).get('function_call'):
# Which function call was invoked
function_called = response_message.function_call.name
# Extracting the arguments
function_args = json.loads(response_message.function_call.arguments)
# Function names
available_functions = {
"get_customer": get_customer
}
fuction_to_call = available_functions[function_called]
response_message = fuction_to_call(*list(function_args .values()))
else:
response_message = response_message.content
The output is:
Enter your message: i need details of a customer whose ID is ibyW5IZ6nmvaw7nIMMTMz
Bot: ChatCompletionMessage(content=None, role='assistant', function_call=FunctionCall(arguments='{}', name='get_customer'), tool_calls=None)
Traceback (most recent call last):
File "/Users/suganth/Documents/LLM/OpenAI/main.py", line 104, in <module>
response_message = fuction_to_call(*list(function_args .values()))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: get_customer() missing 1 required positional argument: 'customerID'