Home > Backend Development > Python Tutorial > Use a line of Python code to connect ChatGPT to WeChat robots:

Use a line of Python code to connect ChatGPT to WeChat robots:

WBOY
Release: 2023-05-09 23:28:07
forward
1159 people have browsed it

1. Implementation Principle

Perhaps all intelligent chat robots have the following implementation ideas?

First, obtain the chat content through code (no language limit), then transfer the chat content to the AI ​​background, and finally transfer the obtained AI reply to the chat interface via the original route.

Use a line of Python code to connect ChatGPT to WeChat robots:

In this process, the difficulties are:

  • How to obtain the chat content;

  • How to call AI reply;

  • How to automatically reply correctly.

In the process of development, you will find that the last item: how to correct every dialogue is the most difficult.

2. Upload the code

Download PyOfficeRobot

pip install PyOfficeRobot
Copy after login

1 line of code to implement WeChat robot ChatGPT

import PyOfficeRobot
PyOfficeRobot.chat.chat_by_gpt(who='程序员', api_key='你的api_key')
Copy after login

3. Parameter description

Only 2 parameters are listed above:

  • who='Programmer': required, the person you want to reply intelligently;

  • api_key='your api_key': required, your own ChatGPT

In fact, there are a series of parameters inside the method, but our project is mainly used by novices, so I filled them all in If you are a professional developer, you can refer to the instructions below to modify it. (from OpenAI official website)

    completions = openai.Completion.create(
        engine=model_engine,
        prompt=prompt,
        max_tokens=max_tokens,  # 生成结果时的最大 tokens 数。平均一个汉字是 2 个 tokens,text-davinci-003 最多是 4000 个 tokens,也就是 2000 个汉字左右
        n=n,
        stop=stop,
        temperature=temperature,  # 控制结果的随机性,如果希望结果更有差异性 0.9,或者希望有固定结果可以尝试 0.0
        top_p=top_p,  # 一个可用于代替 temperature 的参数,对应机器学习中 nucleus sampling,如果设置 0.1 意味着只考虑构成前 10% 概率质量的 tokens
        frequency_penalty=frequency_penalty,  # 控制字符的重复度,取值为 -2.0 ~ 2.0 之间的数字
        presence_penalty=presence_penalty  # 控制主题的重复度,取值为 -2.0 ~ 2.0 之间的数字
    )
Copy after login

The above is the detailed content of Use a line of Python code to connect ChatGPT to WeChat robots:. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template