{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new ConversationChain chain...\u001b[0m\n", "Prompt after formatting:\n", "\u001b[32;1m\u001b[1;3mThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n", "\n", "Current conversation:\n", "\n", "Human: 你好\n", "AI:\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "' 你好!很高兴见到你!我是一个AI,我可以为你提供有关我所在地区的信息。你想知道什么?'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from langchain.llms import OpenAI\n", "from langchain.chains import ConversationChain\n", "from langchain.memory import ConversationBufferMemory\n", "\n", "llm = OpenAI(temperature=0.5)\n", "chain = ConversationChain(\n", " llm=llm,\n", " verbose=True,\n", " memory=ConversationBufferMemory(ai_prefix=\"xiaobang\")\n", ")\n", "chain.predict(input=\"你好\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new ConversationChain chain...\u001b[0m\n", "Prompt after formatting:\n", "\u001b[32;1m\u001b[1;3mThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n", "\n", "Current conversation:\n", "Human: 你好\n", "xiaobang: 你好!很高兴见到你!我是一个AI,我可以为你提供有关我所在地区的信息。你想知道什么?\n", "Human: 北京天气怎么样\n", "AI:\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "' 北京的天气现在很好!今天是晴天,温度约为20摄氏度,并且有轻微的风。'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chain.predict(input=\"北京天气怎么样\")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new ConversationChain chain...\u001b[0m\n", "Prompt after formatting:\n", "\u001b[32;1m\u001b[1;3mThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n", "\n", "Current conversation:\n", "Human: 你好\n", "xiaobang: 你好!很高兴见到你!我是一个AI,我可以为你提供有关我所在地区的信息。你想知道什么?\n", "Human: 北京天气怎么样\n", "xiaobang: 北京的天气现在很好!今天是晴天,温度约为20摄氏度,并且有轻微的风。\n", "Human: 天津呢\n", "AI:\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "' 对不起,我不知道天津的天气情况。'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chain.predict(input=\"天津呢\")" ] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.10" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }