mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-25 18:54:30 +00:00
if "function_call" find name
This commit is contained in:
parent
d2bdfecd96
commit
404af1be0f
5 changed files with 272 additions and 2 deletions
BIN
dist/litellm-0.1.687-py3-none-any.whl
vendored
BIN
dist/litellm-0.1.687-py3-none-any.whl
vendored
Binary file not shown.
BIN
dist/litellm-0.1.687.tar.gz
vendored
BIN
dist/litellm-0.1.687.tar.gz
vendored
Binary file not shown.
|
@ -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:
|
||||||
|
|
249
litellm/tests/test_response.json
Normal file
249
litellm/tests/test_response.json
Normal 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"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
|
@ -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 = []
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue