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,69 +101,87 @@ def extract_reasoning_and_calls(chunks: list): ...@@ -109,69 +101,87 @@ 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(
model=model, api_key=openai_api_key,
tools=tools) base_url=openai_api_base,
print(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}") models = client.models.list()
print(f"function arguments: " model = models.data[0].id
f"{tool_calls.choices[0].message.tool_calls[0].function.arguments}")
print(
print("----------Stream Generate With Automatic Function Calling-----------") "---------Full Generate With Automatic Function Calling-------------")
tool_calls_stream = client.chat.completions.create(messages=messages, tool_calls = client.chat.completions.create(messages=messages,
model=model, model=model,
tools=tools, tools=tools)
stream=True) print(
chunks = [] f"reasoning_content: {tool_calls.choices[0].message.reasoning_content}"
for chunk in tool_calls_stream: )
chunks.append(chunk) print(f"function name: "
f"{tool_calls.choices[0].message.tool_calls[0].function.name}")
reasoning_content, arguments, function_names = extract_reasoning_and_calls( print(f"function arguments: "
chunks) f"{tool_calls.choices[0].message.tool_calls[0].function.arguments}")
print(f"reasoning_content: {reasoning_content}") print(
print(f"function name: {function_names[0]}") "----------Stream Generate With Automatic Function Calling-----------")
print(f"function arguments: {arguments[0]}") tool_calls_stream = client.chat.completions.create(messages=messages,
model=model,
print("----------Full Generate With Named Function Calling-----------------") tools=tools,
tool_calls = client.chat.completions.create(messages=messages, stream=True)
model=model,
tools=tools, chunks = list(tool_calls_stream)
tool_choice={
"type": "function", reasoning_content, arguments, function_names = extract_reasoning_and_calls(
"function": { chunks)
"name":
"get_current_weather" print(f"reasoning_content: {reasoning_content}")
} print(f"function name: {function_names[0]}")
}) print(f"function arguments: {arguments[0]}")
tool_call = tool_calls.choices[0].message.tool_calls[0].function print(
print(f"reasoning_content: {tool_calls.choices[0].message.reasoning_content}") "----------Full Generate With Named Function Calling-----------------")
print(f"function name: {tool_call.name}") tool_calls = client.chat.completions.create(messages=messages,
print(f"function arguments: {tool_call.arguments}") model=model,
print("----------Stream Generate With Named Function Calling--------------") tools=tools,
tool_choice={
tool_calls_stream = client.chat.completions.create( "type": "function",
messages=messages, "function": {
model=model, "name":
tools=tools, "get_current_weather"
tool_choice={ }
"type": "function", })
"function": {
"name": "get_current_weather" tool_call = tool_calls.choices[0].message.tool_calls[0].function
} print(
}, f"reasoning_content: {tool_calls.choices[0].message.reasoning_content}"
stream=True) )
print(f"function name: {tool_call.name}")
chunks = [] print(f"function arguments: {tool_call.arguments}")
for chunk in tool_calls_stream: print(
chunks.append(chunk) "----------Stream Generate With Named Function Calling--------------")
reasoning_content, arguments, function_names = extract_reasoning_and_calls( tool_calls_stream = client.chat.completions.create(
chunks) messages=messages,
print(f"reasoning_content: {reasoning_content}") model=model,
print(f"function name: {function_names[0]}") tools=tools,
print(f"function arguments: {arguments[0]}") tool_choice={
print("\n\n") "type": "function",
"function": {
"name": "get_current_weather"
}
},
stream=True)
chunks = list(tool_calls_stream)
reasoning_content, arguments, function_names = extract_reasoning_and_calls(
chunks)
print(f"reasoning_content: {reasoning_content}")
print(f"function name: {function_names[0]}")
print(f"function arguments: {arguments[0]}")
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