使用当前提示将最近的历史记录传递给LLM

问题描述 投票:0回答:1

我有下面的 azure openai 代码,我在 python 3.10 中运行。 在 bot_response 函数中,我传递用户的查询并从 LLM 获取响应。 我想修改代码,以便在收到提示时,LLM 将获取用户最近三个查询的完整历史记录以及 LLM 的响应。 我想修改代码,以便法学硕士了解用户和法学硕士之间最近的聊天历史记录。 我知道lanchain代理有一个内存缓冲区选项可以实现类似的效果。 有没有类似的东西可以完成 azure openai 聊天? 或者有人可以建议如何做到这一点吗?

代码:

from openai import AzureOpenAI

def bot_response(query):
    client = AzureOpenAI(
    api_key=xxxx,
    azure_deployment=xxxxx,
    azure_endpoint=xxxxx,
    api_version=xxxx
    )


    chat_completion = client.chat.completions.create(
    messages=[
    {
    "role":"system",
    "content":("Instructions:\n"
    "do stuff\n"
    )
    },
    {
    "role":"user",
    "content":query,
    }
    ],
    model="gpt-4",
    response_format={"type":"json_object"}
    )
    return chat_completion.choices[0].message.content
python-3.x large-language-model azure-openai
1个回答
0
投票

您可以只拥有一个聊天历史记录数组,当提示新聊天时,使用输入和输出更新数组。像这样的东西可能会起作用

from openai import AzureOpenAI

history = []

def bot_response(query):
    client = AzureOpenAI(
        api_key="xxxx",
        azure_deployment="xxxxx",
        azure_endpoint="xxxxx",
        api_version="xxxx"
    )

history.append({"role": "user", "content": query})
recent_history = history[-7:]

system_message = {
    "role": "system",
    "content": (
        "Instructions:\n"
        "do stuff\n"
    )
}

chat_completion = client.chat.completions.create(
    messages=[system_message] + recent_history,
    model="gpt-4",
    response_format={"type": "json_object"}
)

bot_reply = chat_completion.choices[0].message.content
history.append({"role": "assistant", "content": bot_reply})

return bot_reply
© www.soinside.com 2019 - 2024. All rights reserved.