noocoder777
noocoder777

Reputation: 62

How do I stream the response when function calling is used in OpenAI API?

I'm using function calling in OpenAI to get search results from web to give OpenAi's gpt-3.5-turbo model some context when answering questions. I want to stream the response as soon as it becomes available. In the code below, when functions are used,the response is correctly streamed but on some occasions where gpt-3.5-turbo doesn't need to use tools to search the web, the response is not streamed. I want to stream all responses from gpt-3.5-turbo, regardless if it uses any tools or not.

Below is my code:


from datetime import datetime
import openai
import json
import requests


# Set the OpenAI API key
openai.api_key = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

# Define a dummy function
def search_web(query):
   '''
   get search results
   '''
    result = "test"
    return result

# Define the function call
function_list=[
        {
            "type": "function",
            "function": {
                "name": "search_web",
                "description": "Get search results for a given query",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "query": {
                            "type": "string",
                            "description": "complete search query",
                        },
                       
                    },
                    "required": ["query"],
                },
            },
        }
    ]

# Define the messages
messages = [
    {
        "role": "system",
        "content": "You are a helpful assistant.",
    },
    {
        "role": "user",
        "content": "What is the latest news?",
    },
   
]

# Call the OpenAI API with function calling
response = openai.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=messages,
    tools=function_list
)   

# Extract the assistant's reply
response_message = response.choices[0].message
print(response_message)
tool_calls = response_message.tool_calls
    # Step 2: check if the model wanted to call a function
if tool_calls:
    # Step 3: call the function
    # Note: the JSON response may not always be valid; be sure to handle errors
    available_functions = {
        "search_web": search_web,
    }  # only one function in this example, but you can have multiple
    messages.append(response_message)  # extend conversation with assistant's reply
    # Step 4: send the info for each function call and function response to the model
    for tool_call in tool_calls:
        function_name = tool_call.function.name
        function_to_call = available_functions[function_name]
        function_args = json.loads(tool_call.function.arguments)
        function_response = function_to_call(
            query=function_args.get("query"),
            
        )
        messages.append(
            {
                "tool_call_id": tool_call.id,
                "role": "tool",
                "name": function_name,
                "content": function_response,
            }
        )  # extend conversation with function response
    stream = openai.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=messages,
        stream=True
    )  # get a new response from the model where it can see the function response
    
    full_response= ""
    for chunk in stream:
        if chunk is not None:
            chunk = chunk.choices[0].delta.content
            chunk = str(chunk)
            if chunk != "None":
                print(chunk)
                full_response += chunk
            else:     
                print("Printing full_response ", full_response) 



Upvotes: 1

Views: 738

Answers (0)

Related Questions