mirror of
https://git.datalinker.icu/vllm-project/vllm.git
synced 2025-12-14 07:25:01 +08:00
[Misc] refactor example series (#16972)
Signed-off-by: reidliu41 <reid201711@gmail.com> Co-authored-by: reidliu41 <reid201711@gmail.com>
This commit is contained in:
parent
0e237f0035
commit
4b91c927f6
@ -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):
|
|||||||
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}")
|
|
||||||
print(f"function arguments: "
|
|
||||||
f"{tool_calls.choices[0].message.tool_calls[0].function.arguments}")
|
|
||||||
|
|
||||||
print("----------Stream Generate With Automatic Function Calling-----------")
|
models = client.models.list()
|
||||||
tool_calls_stream = client.chat.completions.create(messages=messages,
|
model = models.data[0].id
|
||||||
model=model,
|
|
||||||
tools=tools,
|
|
||||||
stream=True)
|
|
||||||
chunks = []
|
|
||||||
for chunk in tool_calls_stream:
|
|
||||||
chunks.append(chunk)
|
|
||||||
|
|
||||||
reasoning_content, arguments, function_names = extract_reasoning_and_calls(
|
print(
|
||||||
chunks)
|
"---------Full Generate With Automatic Function Calling-------------")
|
||||||
|
tool_calls = client.chat.completions.create(messages=messages,
|
||||||
|
model=model,
|
||||||
|
tools=tools)
|
||||||
|
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}")
|
||||||
|
print(f"function arguments: "
|
||||||
|
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,
|
||||||
|
tools=tools,
|
||||||
|
stream=True)
|
||||||
|
|
||||||
print("----------Full Generate With Named Function Calling-----------------")
|
chunks = list(tool_calls_stream)
|
||||||
tool_calls = client.chat.completions.create(messages=messages,
|
|
||||||
model=model,
|
|
||||||
tools=tools,
|
|
||||||
tool_choice={
|
|
||||||
"type": "function",
|
|
||||||
"function": {
|
|
||||||
"name":
|
|
||||||
"get_current_weather"
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
tool_call = tool_calls.choices[0].message.tool_calls[0].function
|
reasoning_content, arguments, function_names = extract_reasoning_and_calls(
|
||||||
print(f"reasoning_content: {tool_calls.choices[0].message.reasoning_content}")
|
chunks)
|
||||||
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(
|
print(f"reasoning_content: {reasoning_content}")
|
||||||
messages=messages,
|
print(f"function name: {function_names[0]}")
|
||||||
model=model,
|
print(f"function arguments: {arguments[0]}")
|
||||||
tools=tools,
|
|
||||||
tool_choice={
|
|
||||||
"type": "function",
|
|
||||||
"function": {
|
|
||||||
"name": "get_current_weather"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
stream=True)
|
|
||||||
|
|
||||||
chunks = []
|
print(
|
||||||
for chunk in tool_calls_stream:
|
"----------Full Generate With Named Function Calling-----------------")
|
||||||
chunks.append(chunk)
|
tool_calls = client.chat.completions.create(messages=messages,
|
||||||
|
model=model,
|
||||||
|
tools=tools,
|
||||||
|
tool_choice={
|
||||||
|
"type": "function",
|
||||||
|
"function": {
|
||||||
|
"name":
|
||||||
|
"get_current_weather"
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
reasoning_content, arguments, function_names = extract_reasoning_and_calls(
|
tool_call = tool_calls.choices[0].message.tool_calls[0].function
|
||||||
chunks)
|
print(
|
||||||
print(f"reasoning_content: {reasoning_content}")
|
f"reasoning_content: {tool_calls.choices[0].message.reasoning_content}"
|
||||||
print(f"function name: {function_names[0]}")
|
)
|
||||||
print(f"function arguments: {arguments[0]}")
|
print(f"function name: {tool_call.name}")
|
||||||
print("\n\n")
|
print(f"function arguments: {tool_call.arguments}")
|
||||||
|
print(
|
||||||
|
"----------Stream Generate With Named Function Calling--------------")
|
||||||
|
|
||||||
|
tool_calls_stream = client.chat.completions.create(
|
||||||
|
messages=messages,
|
||||||
|
model=model,
|
||||||
|
tools=tools,
|
||||||
|
tool_choice={
|
||||||
|
"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()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user