guidance-ai / guidance

A guidance language for controlling large language models.
MIT License
18.79k stars 1.04k forks source link

KeyError: 'content' in _openai.py add_text_to_chat_mode when using Azure gpt-3.5-turbo 2023-07-01-preview #332

Open lowcountrylee opened 1 year ago

lowcountrylee commented 1 year ago

The bug This could be an issue on the other end but it appears that at the moment the Azure gpt-3.5-turbo 2023-07-01-preview function does not set message.content when using the function capability.

To Reproduce

import guidance

llm = guidance.llms.OpenAI("gpt-3.5-turbo", api_type='azure', api_key="...", api_base="...", api_version="2023-07-01-preview", deployment_id="...", caching=True)

guidance.llm = llm

# the rest of the code is from the get_current_weather example 

System info (please complete the following information):

lowcountrylee commented 1 year ago

I see the variables set in:

image

I'm guessing the issue is that chat_mode is not coming back at the right type as I see the function_call code inside of add_text_to_chat_mode_generator.

lowcountrylee commented 1 year ago

The issue is that I'm not using streaming and the function logic seems to only be in the streaming handler. I resolved it in my instance by changing the function:


def add_text_to_chat_mode(chat_mode):
    if isinstance(chat_mode, (types.AsyncGeneratorType, types.GeneratorType)):
        return add_text_to_chat_mode_generator(chat_mode)
    else:
        for c in chat_mode['choices']:

            if "content" in c['message'] and c['message']['content'] != "":
                c['text'] = c['message']['content']

            # capture function call data and convert to text again so we have a consistent interface with non-chat mode and open models
            if "function_call" in c['message']:

                # build the start of the function call (the follows the syntax that GPT says it wants when we ask it, and will be parsed by the @function_detector)
                c['text'] = "\n```typescript\nfunctions." + c['message']['function_call']["name"] + "(" + c['message']['function_call']["arguments"].replace("\\n", "\n") + ")```"

        return chat_mode
MichaelOwenDyer commented 1 year ago

Just encountered this issue with GPT-4-32k on Azure