Git Product home page Git Product logo

Comments (3)

upbit avatar upbit commented on July 24, 2024

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.

danyang-rainbow avatar danyang-rainbow commented on July 24, 2024

最好加入重试次数,超时时间等模块。Langchain也是有这些的

from zhipuai-sdk-python.

yuanjie-ai avatar yuanjie-ai commented on July 24, 2024

跪求star

image

from zhipuai-sdk-python.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.