Iterate with function calling

Hi there, I’m a newbie with OpenAI API.

I want to add a simple loop user query to the function-calling sample found in the API documentation. Unfortunately, it crashes after the second input is passed. I’m not really aware of what is going on here. I have been reading about it in forums and tried different approaches but always failed.

I have found different ways of calling chat completion, which confuses me even more.

Can someone guide me?

import json
import openai
import requests
from tenacity import retry, wait_random_exponential, stop_after_attempt
from termcolor import colored

GPT_MODEL = "gpt-3.5-turbo-0613"

# Importar las APIs
import a_env_vars
openai.api_key = a_env_vars.OPENAI_API_KEY

@retry(wait=wait_random_exponential(multiplier=1, max=40), stop=stop_after_attempt(3))
def chat_completion_request(messages, tools=None, tool_choice=None, model=GPT_MODEL):
    headers = {
        "Content-Type": "application/json",
        "Authorization": "Bearer " + openai.api_key,
    }
    json_data = {"model": model, "messages": messages}
    if tools is not None:
        json_data.update({"tools": tools})
    if tool_choice is not None:
        json_data.update({"tool_choice": tool_choice})
    try:
        response = requests.post(
            "https://api.openai.com/v1/chat/completions",
            headers=headers,
            json=json_data,
        )
        return response
    except Exception as e:
        print("Unable to generate ChatCompletion response")
        print(f"Exception: {e}")
        return e


def pretty_print_conversation(messages):
    role_to_color = {
        "system": "red",
        "user": "green",
        "assistant": "blue",
        "tool": "magenta",
    }
    
    for message in messages:
        if message["role"] == "system":
            print(colored(f"system: {message['content']}\n", role_to_color[message["role"]]))
        elif message["role"] == "user":
            print(colored(f"user: {message['content']}\n", role_to_color[message["role"]]))
        elif message["role"] == "assistant" and message.get("function_call"):
            print(colored(f"assistant: {message['function_call']}\n", role_to_color[message["role"]]))
        elif message["role"] == "assistant" and not message.get("function_call"):
            print(colored(f"assistant: {message['content']}\n", role_to_color[message["role"]]))
        elif message["role"] == "tool":
            print(colored(f"function ({message['name']}): {message['content']}\n", role_to_color[message["role"]]))


def get_current_weather(location, format):
    return f"return current : {location}, {format}"


def get_n_day_weather_forecast(location, format, num_days):  
    return f"return n_day : {location}, {format}, ¨{num_days} "


tools = [
    {
        "type": "function",
        "function": {
            "name": "get_current_weather",
            "description": "Get the current weather",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "The city and state, e.g. San Francisco, CA",
                    },
                    "format": {
                        "type": "string",
                        "enum": ["celsius", "fahrenheit"],
                        "description": "The temperature unit to use. Infer this from the users location.",
                    },
                },
                "required": ["location", "format"],
            },
        }
    },
    {
        "type": "function",
        "function": {
            "name": "get_n_day_weather_forecast",
            "description": "Get an N-day weather forecast",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "The city and state, e.g. San Francisco, CA",
                    },
                    "format": {
                        "type": "string",
                        "enum": ["celsius", "fahrenheit"],
                        "description": "The temperature unit to use. Infer this from the users location.",
                    },
                    "num_days": {
                        "type": "integer",
                        "description": "The number of days to forecast",
                    }
                },
                "required": ["location", "format", "num_days"]
            },
        }
    },
]

def chat_with_assistant():
    messages = []
    
    initial_message = "Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous."
    print("Assistant:", initial_message)
    messages.append({"role": "system", "content": initial_message})
    
    while True:
        user_question = input("You: ")  
        messages.append({"role": "user", "content": user_question})  
        chat_response = chat_completion_request(messages, tools=tools)
        #print(chat_response)
        assistant_message = chat_response.json()["choices"][0]["message"]  
       
        messages.append({"role": "assistant", "content": assistant_message})  
        assistant_message
        print("Assistant:", assistant_message)  

chat_with_assistant()