glm4/basic_demo/openai_api_request.py

213 lines
6.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
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
import base64
base_url = "http://127.0.0.1:8000/v1/"
client = OpenAI(api_key="EMPTY", base_url=base_url)
def function_chat(use_stream=False):
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",
# }
]
tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"format": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "The temperature unit to use. Infer this from the users location.",
},
},
"required": ["location", "format"],
},
}
},
]
# All Tools: CogView
# messages = [{"role": "user", "content": "帮我画一张天空的画画吧"}]
# tools = [{"type": "cogview"}]
# All Tools: Searching
# messages = [{"role": "user", "content": "今天黄金的价格"}]
# tools = [{"type": "simple_browser"}]
response = client.chat.completions.create(
model="glm-4",
messages=messages,
tools=tools,
stream=use_stream,
max_tokens=256,
temperature=0.9,
presence_penalty=1.2,
top_p=0.1,
tool_choice="auto"
)
if response:
if use_stream:
for chunk in response:
print(chunk)
else:
print(response)
else:
print("Error:", response.status_code)
def simple_chat(use_stream=False):
messages = [
{
"role": "user",
"content": "请在你输出的时候都带上“喵喵喵”三个字,放在开头。",
},
{
"role": "user",
"content": "你是猫吗"
}
]
response = client.chat.completions.create(
model="glm-4",
messages=messages,
stream=use_stream,
max_tokens=256,
temperature=0.4,
presence_penalty=1.2,
top_p=0.8,
)
if response:
if use_stream:
for chunk in response:
print(chunk)
else:
print(response)
else:
print("Error:", response.status_code)
def create_chat_completion(messages, use_stream=False):
response = client.chat.completions.create(
model="glm-4v",
messages=messages,
stream=use_stream,
max_tokens=256,
temperature=0.4,
presence_penalty=1.2,
top_p=0.8,
)
if response:
if use_stream:
for chunk in response:
print(chunk)
else:
print(response)
else:
print("Error:", response.status_code)
def encode_image(image_path):
"""
Encodes an image file into a base64 string.
Args:
image_path (str): The path to the image file.
This function opens the specified image file, reads its content, and encodes it into a base64 string.
The base64 encoding is used to send images over HTTP as text.
"""
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
def glm4v_simple_image_chat(use_stream=False, img_path=None):
"""
Facilitates a simple chat interaction involving an image.
Args:
use_stream (bool): Specifies whether to use streaming for chat responses.
img_path (str): Path to the image file to be included in the chat.
This function encodes the specified image and constructs a predefined conversation involving the image.
It then calls `create_chat_completion` to generate a response from the model.
The conversation includes asking about the content of the image and a follow-up question.
"""
img_url = f"data:image/jpeg;base64,{encode_image(img_path)}"
messages = [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Whats in this image?",
},
{
"type": "image_url",
"image_url": {
"url": img_url
},
},
],
},
{
"role": "assistant",
"content": "The image displays a wooden boardwalk extending through a vibrant green grassy wetland. The sky is partly cloudy with soft, wispy clouds, indicating nice weather. Vegetation is seen on either side of the boardwalk, and trees are present in the background, suggesting that this area might be a natural reserve or park designed for ecological preservation and outdoor recreation. The boardwalk allows visitors to explore the area without disturbing the natural habitat.",
},
{
"role": "user",
"content": "Do you think this is a spring or winter photo?"
},
]
create_chat_completion(messages=messages, use_stream=use_stream)
if __name__ == "__main__":
# Testing the text model
simple_chat(use_stream=False)
# Testing the text model with tools
# function_chat(use_stream=False)
# Testing images of multimodal models
# glm4v_simple_image_chat(use_stream=False, img_path="demo.jpg")