Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functions: Arguments are absent when called via ChatCompletion.acreate #488

Closed
valaises opened this issue Jun 14, 2023 · 2 comments
Closed
Labels
bug Something isn't working

Comments

@valaises
Copy link

valaises commented Jun 14, 2023

Describe the bug

Hi! If you create a chat using acreate, provide functions, up-to-date model, the arguments field is always empty regardless, reproduces every single time. Altho, if you are using simple synchronous create on the same setup, it works perfectly.

To Reproduce

ChatCompletion.acreate

gen = await openai.ChatCompletion.acreate(
    model='gpt-3.5-turbo-0613',
    messages=[{"role": "user", "content": "What's the weather like in Boston?"}],
    stream=True,
    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",
                    },
                    "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
                },
                "required": ["location"],
            },
        }
    ],
    function_call='auto'
)

  resp = gen.__anext__()
  delta = resp.choices[0].delta
  print(f'DELTA:\n{delta}')

OUTPUT (ARGUMENTS ARE ABSENT)

DELTA:
{
  "role": "assistant",
  "content": null,
  "function_call": {
    "name": "get_current_weather",
    "arguments": ""
  }
}

ChatCompletion.create

response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo-0613",
    messages=[{"role": "user", "content": "What's the weather like in Boston?"}],
    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",
                    },
                    "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
                },
                "required": ["location"],
            },
        }
    ],
    function_call="auto",
)

message = response["choices"][0]["message"]
print(message)

OUTPUT (ARGUMENTS ARE PRESENT)

{
  "role": "assistant",
  "content": null,
  "function_call": {
    "name": "get_current_weather",
    "arguments": "{\n  \"location\": \"Boston\"\n}"
  }
}

Code snippets

No response

OS

Ubuntu 20.04.5 LTS

Python version

Python 3.9.15

Library version

openai-python v0.27.8

@valaises valaises added the bug Something isn't working label Jun 14, 2023
@n3d1117
Copy link

n3d1117 commented Jun 17, 2023

Hey @valaises, just chiming in since I also bumped into this a few days ago. When using stream=True you need to wait for all the chunks to be sent and combine all the deltas. This works for me:

gen = await openai.ChatCompletion.acreate(
  model='gpt-3.5-turbo-0613',
  messages=[{"role": "user", "content": "What's the weather like in Boston?"}],
  stream=True,
  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",
                  },
                  "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
              },
              "required": ["location"],
          },
      }
  ],
  function_call='auto'
)

function_name = ''
arguments = ''
async for item in gen:
  delta = item.choices[0].delta
  if delta.get('function_call'):
      function_name += delta.function_call.get('name', '')
      arguments += str(delta.function_call.get('arguments', ''))

print(f'function_name: {function_name}\narguments: {json.loads(arguments)}')

Output:

function_name: get_current_weather
arguments: {'location': 'Boston, MA'}

@valaises
Copy link
Author

valaises commented Jun 17, 2023

Hi @n3d1117, thanks for your help and solution! It helped 👍 👍 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants