"vscode:/vscode.git/clone" did not exist on "6bd1dd9d2625ac0ac81e8b5c740aa855399990b9"
Unverified Commit 4b91c927 authored by Reid's avatar Reid Committed by GitHub
Browse files

[Misc] refactor example series (#16972)


Signed-off-by: default avatarreidliu41 <reid201711@gmail.com>
Co-authored-by: default avatarreidliu41 <reid201711@gmail.com>
parent 0e237f00
...@@ -31,14 +31,6 @@ available_tools = {"get_current_weather": get_current_weather} ...@@ -31,14 +31,6 @@ available_tools = {"get_current_weather": get_current_weather}
openai_api_key = "EMPTY" openai_api_key = "EMPTY"
openai_api_base = "http://localhost:8000/v1" openai_api_base = "http://localhost:8000/v1"
client = OpenAI(
api_key=openai_api_key,
base_url=openai_api_base,
)
models = client.models.list()
model = models.data[0].id
tools = [{ tools = [{
"type": "function", "type": "function",
"function": { "function": {
...@@ -109,34 +101,47 @@ def extract_reasoning_and_calls(chunks: list): ...@@ -109,34 +101,47 @@ def extract_reasoning_and_calls(chunks: list):
return reasoning_content, arguments, function_names return reasoning_content, arguments, function_names
print("---------Full Generate With Automatic Function Calling-------------") def main():
tool_calls = client.chat.completions.create(messages=messages, client = OpenAI(
api_key=openai_api_key,
base_url=openai_api_base,
)
models = client.models.list()
model = models.data[0].id
print(
"---------Full Generate With Automatic Function Calling-------------")
tool_calls = client.chat.completions.create(messages=messages,
model=model, model=model,
tools=tools) tools=tools)
print(f"reasoning_content: {tool_calls.choices[0].message.reasoning_content}") print(
print(f"function name: " f"reasoning_content: {tool_calls.choices[0].message.reasoning_content}"
)
print(f"function name: "
f"{tool_calls.choices[0].message.tool_calls[0].function.name}") f"{tool_calls.choices[0].message.tool_calls[0].function.name}")
print(f"function arguments: " print(f"function arguments: "
f"{tool_calls.choices[0].message.tool_calls[0].function.arguments}") f"{tool_calls.choices[0].message.tool_calls[0].function.arguments}")
print("----------Stream Generate With Automatic Function Calling-----------") print(
tool_calls_stream = client.chat.completions.create(messages=messages, "----------Stream Generate With Automatic Function Calling-----------")
tool_calls_stream = client.chat.completions.create(messages=messages,
model=model, model=model,
tools=tools, tools=tools,
stream=True) stream=True)
chunks = []
for chunk in tool_calls_stream:
chunks.append(chunk)
reasoning_content, arguments, function_names = extract_reasoning_and_calls( chunks = list(tool_calls_stream)
reasoning_content, arguments, function_names = extract_reasoning_and_calls(
chunks) chunks)
print(f"reasoning_content: {reasoning_content}") print(f"reasoning_content: {reasoning_content}")
print(f"function name: {function_names[0]}") print(f"function name: {function_names[0]}")
print(f"function arguments: {arguments[0]}") print(f"function arguments: {arguments[0]}")
print("----------Full Generate With Named Function Calling-----------------") print(
tool_calls = client.chat.completions.create(messages=messages, "----------Full Generate With Named Function Calling-----------------")
tool_calls = client.chat.completions.create(messages=messages,
model=model, model=model,
tools=tools, tools=tools,
tool_choice={ tool_choice={
...@@ -147,13 +152,16 @@ tool_calls = client.chat.completions.create(messages=messages, ...@@ -147,13 +152,16 @@ tool_calls = client.chat.completions.create(messages=messages,
} }
}) })
tool_call = tool_calls.choices[0].message.tool_calls[0].function tool_call = tool_calls.choices[0].message.tool_calls[0].function
print(f"reasoning_content: {tool_calls.choices[0].message.reasoning_content}") print(
print(f"function name: {tool_call.name}") f"reasoning_content: {tool_calls.choices[0].message.reasoning_content}"
print(f"function arguments: {tool_call.arguments}") )
print("----------Stream Generate With Named Function Calling--------------") print(f"function name: {tool_call.name}")
print(f"function arguments: {tool_call.arguments}")
print(
"----------Stream Generate With Named Function Calling--------------")
tool_calls_stream = client.chat.completions.create( tool_calls_stream = client.chat.completions.create(
messages=messages, messages=messages,
model=model, model=model,
tools=tools, tools=tools,
...@@ -165,13 +173,15 @@ tool_calls_stream = client.chat.completions.create( ...@@ -165,13 +173,15 @@ tool_calls_stream = client.chat.completions.create(
}, },
stream=True) stream=True)
chunks = [] chunks = list(tool_calls_stream)
for chunk in tool_calls_stream:
chunks.append(chunk)
reasoning_content, arguments, function_names = extract_reasoning_and_calls( reasoning_content, arguments, function_names = extract_reasoning_and_calls(
chunks) chunks)
print(f"reasoning_content: {reasoning_content}") print(f"reasoning_content: {reasoning_content}")
print(f"function name: {function_names[0]}") print(f"function name: {function_names[0]}")
print(f"function arguments: {arguments[0]}") print(f"function arguments: {arguments[0]}")
print("\n\n") print("\n\n")
if __name__ == "__main__":
main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment