MiniCPM/demo/openai_api_demo/openai_api_request_demo.py
zR b9b53e2e19 OpenAI 推理简单应用
用的是Linux transformers载入推理的,只测试了常规对话,在跑的时候没有看到支持Function 功能就没写
2024-04-11 19:31:08 +08:00

56 lines
1.4 KiB
Python
Raw 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.

"""
这是一个简单的OpenAI接口代码,由于 MiniCPM-2B的限制该脚本
1. 没有工具调用功能
2. 没有System Prompt
3. 最大支持文本 4096 长度
运行本代码需要:
1. 启动本地服务,本方案使用的是 AutoModelForCausalLM.from_pretrained 读入模型,没有进行优化,可以根据需要自行修改。
2. 通过此代码进行请求。
"""
from openai import OpenAI
base_url = "http://127.0.0.1:8000/v1/"
client = OpenAI(api_key="MiniCPM-2B", base_url=base_url)
def chat(use_stream=True):
messages = [
{
"role": "user",
"content": "tell me a story"
}
]
response = client.chat.completions.create(
model="MiniCPM-2B",
messages=messages,
stream=use_stream,
max_tokens=4096, # need less than 4096 tokens
temperature=0.8,
top_p=0.8
)
if response:
if use_stream:
for chunk in response:
print(chunk.choices[0].delta.content)
else:
content = response.choices[0].message.content
print(content)
else:
print("Error:", response.status_code)
def embedding():
response = client.embeddings.create(
model="bge-m3",
input=["hello, I am MiniCPM-2B"],
)
embeddings = response.data[0].embedding
print("Embedding_Success", len(embeddings))
if __name__ == "__main__":
chat(use_stream=True)