Comments (3)
ChatCompletion.create 基本包装没问题,效果如下:
import zhipuai.openai_bridge as openai
openai.api_key = "65ff******.s******Z"
completion = openai.ChatCompletion.create(
model="chatglm_pro",
messages=[{"role": "user", "content": "你好,请自我介绍并说明你能给我提供的帮助"}]
)
print(completion)
# {
# "code": 200,
# "msg": "\u64cd\u4f5c\u6210\u529f",
# "success": true,
# "request_id": "789********",
# "choices": [
# {
# "message": {
# "role": "assistant",
# "content": "\" \u4f60\u597d\uff01\u6211\u662f ChatGLM\uff0c\u662f\u6e05\u534e\u5927\u5b66 KEG \u5b9e\u9a8c\u5ba4\u548c\u667a\u8c31 AI \u516c\u53f8\u5171\u540c\u8bad\u7ec3\u7684\u8bed\u8a00\u6a21\u578b\u3002\u6211\u7684\u76ee\u6807\u662f\u901a\u8fc7\u56de\u7b54\u7528\u6237\u63d0\u51fa\u7684\u95ee\u9898\u6765\u5e2e\u52a9\u4ed6\u4eec\u89e3\u51b3\u95ee\u9898\u3002\u6211\u53ef\u4ee5\u56de\u7b54\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\u79d1\u5b66\u3001\u6280\u672f\u3001\u5386\u53f2\u3001\u6587\u5316\u3001\u5065\u5eb7\u3001\u6cd5\u5f8b\u7b49\u7b49\u3002\u5982\u679c\u6709\u4efb\u4f55\u95ee\u9898\u9700\u8981\u5e2e\u52a9\uff0c\u8bf7\u968f\u65f6\u5411\u6211\u63d0\u95ee\u3002\""
# },
# "finish_reason": "stop",
# "index": 0
# }
# ]
# }
print(completion.choices[0].message.content)
# " 你好!我是 ChatGLM,是清华大学 KEG 实验室和智谱 AI 公司共同训练的语言模型。我的目标是通过回答用户提出的问题来帮助他们解决问题。我可以回答各种各样的问题,包括但不限于科学、技术、历史、文化、健康、法律等等。如果有任何问题需要帮助,请随时向我提问。"
如果觉得有必要,我提交个MR。
目前局限性:非openai库注入的方式,无法把 ChatCompletion 完全替换。对第三方库需要侵入性修改import才能替换。可能最终还是要部署成API代理才行
一个兼容性比较差但可以工作的方法:利用importhook可以替换掉原生的 openai.ChatCompletion:
import importhook
# Setup hook to be called any time the `openai` module is imported and loaded into module cache
@importhook.on_import('openai')
def on_openai_import(socket):
import openai
new_openai = importhook.copy_module(openai)
setattr(new_openai, 'ChatCompletion', ZPChatCompletion)
return new_openai
# 然后引入其他依赖 openai 的库
# eg in LangChain: import openai
如果有更好的思路,欢迎讨论
from zhipuai-sdk-python.
最好加入重试次数,超时时间等模块。Langchain也是有这些的
from zhipuai-sdk-python.
跪求star
- 兼容langchain openai
- 加入重试逻辑
https://github.com/yuanjie-ai/ChatLLM/blob/master/LLMS.md
from zhipuai-sdk-python.
Related Issues (20)
- API有支持的Function Call的计划吗
- 劳烦给出异步调用后,如何接受真实的response HOT 3
- self._logger.debug("Dispatching %s...", event) HOT 1
- [1234][网络错误,错误id:8168867886816165954,请联系客服。]
- AttributeError: module 'zhipuai' has no attribute 'model_api' HOT 10
- 请问如何调用创建的应用? HOT 1
- 文件上传可否多元化?
- API 启动报错 401 HOT 1
- 官网和github开源的sdk版本差距有点大 HOT 5
- pydantic 依赖冲突 HOT 2
- zhipuai api 如何获取接口调用时间?
- 这个sdk已经废弃了吗
- zhipuai.core._errors.APITimeoutError: Request Timeout
- 调用 ChatGLM-Pro API 接口进行多轮对话问题 HOT 2
- AttributeError: module 'zhipuai' has no attribute 'model_api' HOT 1
- 关于模型`prompt`参数列表长度为奇数的疑问。 HOT 3
- AttributeError: partially initialized module 'zhipuai' has no attribute 'model_api' (most likely due to a circular import)
- 希望用一个asyncio的API
- async_invoke输出如何获取content
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zhipuai-sdk-python.