2024-06-05 10:22:16 +08:00
|
|
|
"""
|
|
|
|
This script creates a OpenAI Request demo for the glm-4-9b model, just Use OpenAI API to interact with the model.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from openai import OpenAI
|
|
|
|
|
|
|
|
base_url = "http://127.0.0.1:8000/v1/"
|
|
|
|
client = OpenAI(api_key="EMPTY", base_url=base_url)
|
|
|
|
|
|
|
|
|
2024-06-08 13:26:43 +08:00
|
|
|
def function_chat(use_stream=False):
|
2024-06-09 16:11:20 +08:00
|
|
|
messages = [
|
|
|
|
{
|
|
|
|
"role": "user", "content": "What's the Celsius temperature in San Francisco?"
|
|
|
|
},
|
|
|
|
|
|
|
|
# Give Observations
|
|
|
|
# {
|
|
|
|
# "role": "assistant",
|
|
|
|
# "content": None,
|
|
|
|
# "function_call": None,
|
|
|
|
# "tool_calls": [
|
|
|
|
# {
|
|
|
|
# "id": "call_1717912616815",
|
|
|
|
# "function": {
|
|
|
|
# "name": "get_current_weather",
|
|
|
|
# "arguments": "{\"location\": \"San Francisco, CA\", \"format\": \"celsius\"}"
|
|
|
|
# },
|
|
|
|
# "type": "function"
|
|
|
|
# }
|
|
|
|
# ]
|
|
|
|
# },
|
|
|
|
# {
|
|
|
|
# "tool_call_id": "call_1717912616815",
|
|
|
|
# "role": "tool",
|
|
|
|
# "name": "get_current_weather",
|
|
|
|
# "content": "23°C",
|
|
|
|
# }
|
|
|
|
]
|
2024-06-05 10:22:16 +08:00
|
|
|
tools = [
|
|
|
|
{
|
|
|
|
"type": "function",
|
|
|
|
"function": {
|
|
|
|
"name": "get_current_weather",
|
2024-06-07 22:14:00 +08:00
|
|
|
"description": "Get the current weather",
|
2024-06-05 10:22:16 +08:00
|
|
|
"parameters": {
|
|
|
|
"type": "object",
|
|
|
|
"properties": {
|
|
|
|
"location": {
|
|
|
|
"type": "string",
|
|
|
|
"description": "The city and state, e.g. San Francisco, CA",
|
|
|
|
},
|
2024-06-07 22:14:00 +08:00
|
|
|
"format": {
|
|
|
|
"type": "string",
|
|
|
|
"enum": ["celsius", "fahrenheit"],
|
|
|
|
"description": "The temperature unit to use. Infer this from the users location.",
|
|
|
|
},
|
2024-06-05 10:22:16 +08:00
|
|
|
},
|
2024-06-07 22:14:00 +08:00
|
|
|
"required": ["location", "format"],
|
2024-06-05 10:22:16 +08:00
|
|
|
},
|
2024-06-07 22:14:00 +08:00
|
|
|
}
|
|
|
|
},
|
2024-06-05 10:22:16 +08:00
|
|
|
]
|
|
|
|
|
2024-06-09 16:11:20 +08:00
|
|
|
# All Tools: CogView
|
2024-06-05 10:22:16 +08:00
|
|
|
# messages = [{"role": "user", "content": "帮我画一张天空的画画吧"}]
|
|
|
|
# tools = [{"type": "cogview"}]
|
2024-06-09 16:11:20 +08:00
|
|
|
|
|
|
|
# All Tools: Searching
|
2024-06-05 10:22:16 +08:00
|
|
|
# messages = [{"role": "user", "content": "今天黄金的价格"}]
|
|
|
|
# tools = [{"type": "simple_browser"}]
|
|
|
|
|
|
|
|
response = client.chat.completions.create(
|
|
|
|
model="glm-4",
|
|
|
|
messages=messages,
|
|
|
|
tools=tools,
|
2024-06-08 13:26:43 +08:00
|
|
|
stream=use_stream,
|
|
|
|
max_tokens=256,
|
|
|
|
temperature=0.9,
|
|
|
|
presence_penalty=1.2,
|
|
|
|
top_p=0.1,
|
2024-06-09 16:11:20 +08:00
|
|
|
tool_choice="auto"
|
2024-06-05 10:22:16 +08:00
|
|
|
)
|
|
|
|
if response:
|
2024-06-08 13:26:43 +08:00
|
|
|
if use_stream:
|
|
|
|
for chunk in response:
|
|
|
|
print(chunk)
|
|
|
|
else:
|
|
|
|
print(response)
|
2024-06-05 10:22:16 +08:00
|
|
|
else:
|
|
|
|
print("Error:", response.status_code)
|
|
|
|
|
|
|
|
|
|
|
|
def simple_chat(use_stream=False):
|
|
|
|
messages = [
|
|
|
|
{
|
|
|
|
"role": "system",
|
2024-06-07 22:14:00 +08:00
|
|
|
"content": "请在你输出的时候都带上“喵喵喵”三个字,放在开头。",
|
2024-06-05 10:22:16 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"role": "user",
|
2024-06-09 16:11:20 +08:00
|
|
|
"content": "你是谁"
|
2024-06-05 10:22:16 +08:00
|
|
|
}
|
|
|
|
]
|
|
|
|
response = client.chat.completions.create(
|
|
|
|
model="glm-4",
|
|
|
|
messages=messages,
|
|
|
|
stream=use_stream,
|
2024-06-07 22:14:00 +08:00
|
|
|
max_tokens=256,
|
2024-06-08 13:26:43 +08:00
|
|
|
temperature=0.4,
|
|
|
|
presence_penalty=1.2,
|
|
|
|
top_p=0.8,
|
|
|
|
)
|
2024-06-05 10:22:16 +08:00
|
|
|
if response:
|
|
|
|
if use_stream:
|
|
|
|
for chunk in response:
|
2024-06-08 13:26:43 +08:00
|
|
|
print(chunk)
|
2024-06-05 10:22:16 +08:00
|
|
|
else:
|
2024-06-08 13:26:43 +08:00
|
|
|
print(response)
|
2024-06-05 10:22:16 +08:00
|
|
|
else:
|
|
|
|
print("Error:", response.status_code)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2024-06-09 16:11:20 +08:00
|
|
|
# simple_chat(use_stream=False)
|
|
|
|
function_chat(use_stream=False)
|
|
|
|
|