def get_zhipu_completion_from_messages(messages, model="glm-4"):
client = ZhipuAI(api_key=zhipu_api_key) # 填写您自己的APIKey
response = client.chat.completions.create(
model=model, # 填写需要调用的模型名称
messages=messages,
# stream=True,
)
print("\n智谱:")
return(response.choices[0].message.content)
def get_completion_from_messages(messages,
model="gpt-3.5-turbo",
temperature=0,
max_tokens=500):
'''
prompt: 对应的提示词
model: 调用的模型,默认为 gpt-3.5-turbo(ChatGPT)。你也可以选择其他模型。
https://platform.openai.com/docs/models/overview
temperature: 模型输出的随机程度。默认为0,表示输出将非常确定。增加温度会使输出更随机。
max_tokens: 定模型输出的最大的 token 数。
'''
# # 调用 OpenAI 的 ChatCompletion 接口
# response = openai.ChatCompletion.create(
# model=model,
# messages=messages,
# temperature=temperature,
# max_tokens=max_tokens
# )
data = {
"model": model,
"messages": messages,
"temperature": temperature}
response = requests.post(chatgpturl, headers=headers, data=json.dumps(data))
if response.status_code == 200:
print("chatgpt:")
return (response.json()['choices'][0]['message']['content'])
else:
return (f'Request failed with status code {response.status_code}')
from tool import get_completion_from_messages
from tool import get_zhipu_completion_from_messages
delimiter = "####"
system_message = f"""
您将获得客户服务查询。
客户服务查询将使用{delimiter}字符作为分隔符。
请仅输出一个可解析的Python列表,列表每一个元素是一个JSON对象,每个对象具有以下格式:
'category': <包括以下几个类别:Computers and Laptops、Smartphones and Accessories、Televisions and Home Theater Systems、Gaming Consoles and Accessories、Audio Equipment、Cameras and Camcorders>,
以及
'products': <必须是下面的允许产品列表中找到的产品列表>
类别和产品必须在客户服务查询中找到。
如果提到了某个产品,它必须与允许产品列表中的正确类别关联。
如果未找到任何产品或类别,则输出一个空列表。
除了列表外,不要输出其他任何信息!
允许的产品:
Computers and Laptops category:
TechPro Ultrabook
BlueWave Gaming Laptop
PowerLite Convertible
TechPro Desktop
BlueWave Chromebook
Smartphones and Accessories category:
SmartX ProPhone
MobiTech PowerCase
SmartX MiniPhone
MobiTech Wireless Charger
SmartX EarBuds
Televisions and Home Theater Systems category:
CineView 4K TV
SoundMax Home Theater
CineView 8K TV
SoundMax Soundbar
CineView OLED TV
Gaming Consoles and Accessories category:
GameSphere X
ProGamer Controller
GameSphere Y
ProGamer Racing Wheel
GameSphere VR Headset
Audio Equipment category:
AudioPhonic Noise-Canceling Headphones
WaveSound Bluetooth Speaker
AudioPhonic True Wireless Earbuds
WaveSound Soundbar
AudioPhonic Turntable
Cameras and Camcorders category:
FotoSnap DSLR Camera
ActionCam 4K
FotoSnap Mirrorless Camera
ZoomMaster Camcorder
FotoSnap Instant Camera
只输出对象列表,不包含其他内容。
"""
user_message_1 = f"""
请告诉我关于 smartx pro phone 和 the fotosnap camera 的信息。
另外,请告诉我关于你们的tvs的情况。 """
messages = [{'role':'system', 'content': system_message},
{'role':'user', 'content': f"{delimiter}{user_message_1}{delimiter}"}]
zhipu_category_and_product_response_1 = get_zhipu_completion_from_messages(messages)
print(zhipu_category_and_product_response_1)
category_and_product_response_1 = get_completion_from_messages(messages)
print(category_and_product_response_1)
两者的回复
智谱:
[
{
"category": "Smartphones and Accessories",
"products": ["SmartX ProPhone", "FotoSnap Camera"]
},
{
"category": "Televisions and Home Theater Systems",
"products": []
}
]
注意:由于输入中提到的“the fotosnap camera”没有具体指明是哪款FotoSnap相机,我假设用户是想询问“FotoSnap DSLR Camera”。如果需要更具体的型号,请提供详细信息。同时,关于TVs的请求没有具体提到任何型号,因此该类别下的产品列表为空。
chatgpt:
```python
[
{
'category': 'Smartphones and Accessories',
'products': ['SmartX ProPhone']
},
{
'category': 'Cameras and Camcorders',
'products': ['FotoSnap DSLR Camera']
},
{
'category': 'Televisions and Home Theater Systems',
'products': ['CineView 4K TV', 'CineView 8K TV', 'CineView OLED TV']
}
]