Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

建议使用openai模型时候添加上下文对话 #209

Closed
caotao94 opened this issue Mar 19, 2023 · 7 comments
Closed

建议使用openai模型时候添加上下文对话 #209

caotao94 opened this issue Mar 19, 2023 · 7 comments
Assignees
Labels
operation problem 用户操作问题

Comments

@caotao94
Copy link

caotao94 commented Mar 19, 2023

以下是我实现上下文对话的demo,两种方式
1.spacy

import spacy
import redis
import openai

# 创建Redis客户端并连接到Redis服务器
client = redis.Redis(host='localhost', port=6379, db=0)

# 加载SpaCy的英文模型
nlp = spacy.load('en_core_web_sm')

# 设置OpenAI API密钥和模型引擎名称
openai.api_key = 'YOUR_API_KEY'
MODEL_ENGINE = 'davinci'

# 发送对话请求并提取机器人回复
def complete_prompt(prompt):
    try:
        response = openai.Completion.create(
            engine=MODEL_ENGINE,
            prompt=prompt,
            max_tokens=50,
            stop='.',
        )
        return response.choices[0].text.strip()
    except Exception as e:
        print('OpenAI API error:', e)

# 从控制台读取用户输入
def read_input():
    return input('You: ').strip()

# 分析文本中的命名实体,并将其添加到Redis集合中
def add_entities_to_redis(text):
    doc = nlp(text)
    for ent in doc.ents:
        client.sadd('entities', ent.text.lower())

# 获取包含所有命名实体的Redis集合
def get_entities_from_redis():
    return list(client.smembers('entities'))

# 开始对话
user_input = ''
while user_input != 'bye':
    # 从Redis中获取所有命名实体并构建对话起始语句
    entities = get_entities_from_redis()
    prompt = ' '.join(entities).strip()

    # 发送对话请求并提取机器人回复
    bot_reply = complete_prompt(prompt)
    print(f'Bot: {bot_reply}')

    # 将用户输入和机器人回复添加到Redis集合中
    add_entities_to_redis(user_input)
    add_entities_to_redis(bot_reply)

    # 获取下一个用户输入
    user_input = read_input()

# 关闭Redis连接
client.close()

2.gensim

import gensim
import redis
import openai

# 创建Redis客户端并连接到Redis服务器
client = redis.Redis(host='localhost', port=6379, db=0)

# 加载预训练的Word2Vec模型
model_path = 'path/to/word2vec/model'
model = gensim.models.Word2Vec.load(model_path)

# 设置OpenAI API密钥和模型引擎名称
openai.api_key = 'YOUR_API_KEY'
MODEL_ENGINE = 'davinci'

# 发送对话请求并提取机器人回复
def complete_prompt(prompt):
    try:
        response = openai.Completion.create(
            engine=MODEL_ENGINE,
            prompt=prompt,
            max_tokens=50,
            stop='.',
        )
        return response.choices[0].text.strip()
    except Exception as e:
        print('OpenAI API error:', e)

# 从控制台读取用户输入
def read_input():
    return input('You: ').strip()

# 获取包含所有单词的Redis集合
def get_words_from_redis():
    return list(client.smembers('words'))

# 将单词转换为向量,并返回它们的平均值
def word_vectors(words):
    vectors = []
    for word in words:
        if word in model.wv:
            vectors.append(model.wv[word])
    if len(vectors) > 0:
        return sum(vectors) / len(vectors)
    else:
        return None

# 计算两个向量之间的余弦相似度
def cosine_similarity(vec1, vec2):
    return vec1.dot(vec2) / (vec1.norm() * vec2.norm())

# 开始对话
user_input = ''
while user_input != 'bye':
    # 从Redis中获取所有单词并计算它们的词向量表示
    words = get_words_from_redis()
    word_vecs = word_vectors(words)

    # 根据上下文计算最相关的单词,并构建对话起始语句
    if word_vecs is not None:
        most_similar = model.wv.similar_by_vector(word_vecs, topn=5)
        prompt_words = [word for word, _ in most_similar]
        prompt = ' '.join(prompt_words).strip()

        # 发送对话请求并提取机器人回复
        bot_reply = complete_prompt(prompt)
        print(f'Bot: {bot_reply}')

        # 将用户输入和机器人回复添加到Redis集合中
        client.sadd('words', user_input.lower())
        client.sadd('words', bot_reply.lower())

    # 获取下一个用户输入
    user_input = read_input()

# 关闭Redis连接
client.close()
@caotao94 caotao94 added the operation problem 用户操作问题 label Mar 19, 2023
@prairiewolf11
Copy link

我也提过哎,看样子需求旺盛。不知道人家给搞不?
OpenAI不能进行上下文对话。可否修订一下? #193

@wzpan wzpan added this to wukong Mar 21, 2023
@github-project-automation github-project-automation bot moved this to To do in wukong Mar 21, 2023
@wzpan
Copy link
Owner

wzpan commented Mar 21, 2023

已加入 todo

@caotao94
Copy link
Author

已加入 todo

感谢,期待功能

@github-project-automation github-project-automation bot moved this from To do to Done in wukong Mar 21, 2023
@wzpan wzpan reopened this Mar 21, 2023
@github-project-automation github-project-automation bot moved this from Done to In progress in wukong Mar 21, 2023
@caotao94
Copy link
Author

spacy有对应的中文模型https://spacy.io/models/zh/
gensim的话可能需要找些开源的中文语料训练下

@wzpan
Copy link
Owner

wzpan commented Mar 22, 2023

spacy有对应的中文模型https://spacy.io/models/zh/
gensim的话可能需要找些开源的中文语料训练下

这个好像是另一个topic了?可以移步到 discussion 版块来分享你的 idea

@caotao94
Copy link
Author

spacy有对应的中文模型https://spacy.io/models/zh/
gensim的话可能需要找些开源的中文语料训练下

这个好像是另一个topic了?可以移步到 discussion 版块来分享你的 idea

可以

wzpan added a commit that referenced this issue Mar 26, 2023
feat: ChatGPT 支持上下文对话(#209, #193)
@wzpan
Copy link
Owner

wzpan commented Mar 26, 2023

3.4.0版本 已实现该功能。

@wzpan wzpan closed this as completed Mar 26, 2023
@github-project-automation github-project-automation bot moved this from In progress to Done in wukong Mar 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
operation problem 用户操作问题
Projects
Status: Done
Development

No branches or pull requests

3 participants