if "function_call" find name

This commit is contained in:
WilliamEspegren 2023-09-17 19:02:50 +02:00
parent d2bdfecd96
commit 404af1be0f
No known key found for this signature in database
GPG key ID: 30E0CDDF1B5262CD
5 changed files with 272 additions and 2 deletions

Binary file not shown.

Binary file not shown.

View file

@ -1158,6 +1158,10 @@ def stream_chunk_builder(chunks: list):
# Extract the "content" strings from the nested dictionaries within "choices" # Extract the "content" strings from the nested dictionaries within "choices"
content_list = [] content_list = []
if "function_call" in chunks[0]["choices"][0]["delta"]:
function_call_name = chunks[0]["choices"][0]["delta"]["function_call"]["name"]
print(function_call_name)
for chunk in chunks: for chunk in chunks:
choices = chunk["choices"] choices = chunk["choices"]
for choice in choices: for choice in choices:

View file

@ -0,0 +1,249 @@
OpenAI non stream response:
{
"id": "chatcmpl-7zoXylnQH7IVUQXpbE5AHZWxQhjCW",
"object": "chat.completion",
"created": 1694966666,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": null,
"function_call": {
"name": "get_current_weather",
"arguments": "{\n\"location\": \"Boston\"\n}"
}
},
"finish_reason": "function_call"
}
],
"usage": {
"prompt_tokens": 82,
"completion_tokens": 15,
"total_tokens": 97
}
}
OpenAI stream response:
{
"id": "chatcmpl-7zoaRRfxJErRzGNgfrqXrTHRexvG7",
"object": "chat.completion.chunk",
"created": 1694966819,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"delta": {
"role": "assistant",
"content": null,
"function_call": {
"name": "get_current_weather",
"arguments": ""
}
},
"finish_reason": null
}
]
}
{
"id": "chatcmpl-7zoaRRfxJErRzGNgfrqXrTHRexvG7",
"object": "chat.completion.chunk",
"created": 1694966819,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"delta": {
"function_call": {
"arguments": "{\n"
}
},
"finish_reason": null
}
]
}
{
"id": "chatcmpl-7zoaRRfxJErRzGNgfrqXrTHRexvG7",
"object": "chat.completion.chunk",
"created": 1694966819,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"delta": {
"function_call": {
"arguments": " "
}
},
"finish_reason": null
}
]
}
{
"id": "chatcmpl-7zoaRRfxJErRzGNgfrqXrTHRexvG7",
"object": "chat.completion.chunk",
"created": 1694966819,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"delta": {
"function_call": {
"arguments": " \""
}
},
"finish_reason": null
}
]
}
{
"id": "chatcmpl-7zoaRRfxJErRzGNgfrqXrTHRexvG7",
"object": "chat.completion.chunk",
"created": 1694966819,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"delta": {
"function_call": {
"arguments": "location"
}
},
"finish_reason": null
}
]
}
{
"id": "chatcmpl-7zoaRRfxJErRzGNgfrqXrTHRexvG7",
"object": "chat.completion.chunk",
"created": 1694966819,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"delta": {
"function_call": {
"arguments": "\":"
}
},
"finish_reason": null
}
]
}
{
"id": "chatcmpl-7zoaRRfxJErRzGNgfrqXrTHRexvG7",
"object": "chat.completion.chunk",
"created": 1694966819,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"delta": {
"function_call": {
"arguments": " \""
}
},
"finish_reason": null
}
]
}
{
"id": "chatcmpl-7zoaRRfxJErRzGNgfrqXrTHRexvG7",
"object": "chat.completion.chunk",
"created": 1694966819,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"delta": {
"function_call": {
"arguments": "Boston"
}
},
"finish_reason": null
}
]
}
{
"id": "chatcmpl-7zoaRRfxJErRzGNgfrqXrTHRexvG7",
"object": "chat.completion.chunk",
"created": 1694966819,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"delta": {
"function_call": {
"arguments": ","
}
},
"finish_reason": null
}
]
}
{
"id": "chatcmpl-7zoaRRfxJErRzGNgfrqXrTHRexvG7",
"object": "chat.completion.chunk",
"created": 1694966819,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"delta": {
"function_call": {
"arguments": " MA"
}
},
"finish_reason": null
}
]
}
{
"id": "chatcmpl-7zoaRRfxJErRzGNgfrqXrTHRexvG7",
"object": "chat.completion.chunk",
"created": 1694966819,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"delta": {
"function_call": {
"arguments": "\"\n"
}
},
"finish_reason": null
}
]
}
{
"id": "chatcmpl-7zoaRRfxJErRzGNgfrqXrTHRexvG7",
"object": "chat.completion.chunk",
"created": 1694966819,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"delta": {
"function_call": {
"arguments": "}"
}
},
"finish_reason": null
}
]
}
{
"id": "chatcmpl-7zoaRRfxJErRzGNgfrqXrTHRexvG7",
"object": "chat.completion.chunk",
"created": 1694966819,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"delta": {},
"finish_reason": "function_call"
}
]
}

View file

@ -2,16 +2,33 @@ from litellm import completion, stream_chunk_builder
import litellm import litellm
import os import os
user_message = "Write a short poem about the sky" user_message = "What is the current weather in Boston?"
messages = [{"content": user_message, "role": "user"}] messages = [{"content": user_message, "role": "user"}]
function_schema = {
"name": "get_weather",
"description":
"gets the current weather",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description":
"The city and state, e.g. San Francisco, CA"
},
},
"required": ["location"]
},
}
def test_stream_chunk_builder(): def test_stream_chunk_builder():
litellm.api_key = os.environ["OPENAI_API_KEY"] litellm.api_key = os.environ["OPENAI_API_KEY"]
response = completion( response = completion(
model="gpt-3.5-turbo", model="gpt-3.5-turbo",
messages=messages, messages=messages,
functions=[function_schema],
stream=True, stream=True,
max_tokens=10,
) )
chunks = [] chunks = []