作者:CSDN博客
Langchain
1、python中安装langchain
- pip install langchain
- pip install langchain-openai
复制代码 登录官网,获取LangSmish的API key
https://smith.langchain.com/settings
2、案例一:创建一个简单的调用大模型的案例:
- import os
- from langchain_core.messages import SystemMessage, HumanMessage
- from langchain_core.output_parsers import StrOutputParser
- from langchain_openai import ChatOpenAI
- os.environ['http_proxy']=''
- os.environ['https_proxy']=''
- os.environ['LANGCHAIN_TRACING_V2']='TRUE'
- os.environ['LANGCHAIN_API_KEY']=''# 创建模型
- model = ChatOpenAI(model="gpt-3.5-turbo-1106")# 准备提示prompt
- msg =[
- SystemMessage(content="请将以下内容翻译成日语"),
- HumanMessage(content="你好,请问你要到哪里去?")]
- result = model.invoke(msg)print(result)# 创建返回数据的解析器
- parser = StrOutputParser()
- return_str = parser.invoke(result)print(return_str)# 得到链(把各个组件连接起来)
- chain = model | parser
- # 直接使用chain来调用print(chain.invoke(msg))
复制代码 结果:- content='こんにちは、どちらに行かれる予定ですか?' additional_kwargs={'refusal':None} response_metadata={'token_usage':{'completion_tokens':16,'prompt_tokens':35,'total_tokens':51,'completion_tokens_details':{'accepted_prediction_tokens':0,'audio_tokens':0,'reasoning_tokens':0,'rejected_prediction_tokens':0},'prompt_tokens_details':{'audio_tokens':0,'cached_tokens':0}},'model_name':'gpt-3.5-turbo-1106','system_fingerprint':'fp_e7d4a5f731','finish_reason':'stop','logprobs':None}id='run-36cc3912-c062-4525-8565-4a30ce2c1b4d-0' usage_metadata={'input_tokens':35,'output_tokens':16,'total_tokens':51,'input_token_details':{'audio':0,'cache_read':0},'output_token_details':{'audio':0,'reasoning':0}}
- こんにちは、どちらに行かれる予定ですか?
- こんにちは、どこに行く予定ですか?
复制代码 构建一个简单的大语言模型(LLM)应用程序
调用语言模型
使用OutPutParsers:输出解析器
- 使用PromptTemplate:提示模板
- # 定义提示模板
- prompt_template = ChatPromptTemplate.from_messages([('system','请将下面的内容翻译成{language}'),('user','{text}')])# 得到链(把各个组件连接起来)(模板 | 模型 | 解析器)
- chain = prompt_template | model | parser
- # 直接使用chain来调用print(chain.invoke({'language':'English','text':'白日依山尽,黄河入海流。'}))
复制代码 使用LangSmish追踪你的应用程序
- 使用LangServe部署你的应用程序
- pip install "langserve[all]"
复制代码- # 把我们的程序部署成服务# 创建fastAPI的应用
- app = FastAPI(title="my LangChain serve", version="V1.0", description="translate any language")# 添加路由
- add_routes(
- app,
- chain,
- path="/chain_demo",)if __name__ =='__main__':import uvicorn
- uvicorn.run(app, host='127.0.0.1', port=8000)
复制代码
通过代码连接服务器,实现对程序的应用- from langserve import RemoteRunnable
- if __name__ =='__main__':
- client = RemoteRunnable("http://127.0.0.1:8000/chain_demo")
- result = client.invoke({'language':"italian",'text':"你好!"})print(result)
复制代码 结果:3、案例二:Langchain构建聊天机器人
聊天机器人能够进行对话,并记住之前的互动
安装:- pip install langchain_community
复制代码 机器人根据上下文来回答问题:- import os
- from langchain_community.chat_message_histories import ChatMessageHistory
- from langchain_core.messages import HumanMessage
- from langchain_core.output_parsers import StrOutputParser
- from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
- from langchain_core.runnables import RunnableWithMessageHistory
- from langchain_openai import ChatOpenAI
- # vpn
- os.environ['http_proxy']='127.0.0.1:7890'
- os.environ['https_proxy']='127.0.0.1:7890'# 连接langchain
- os.environ['LANGCHAIN_TRACING_V2']='TRUE'
- os.environ['LANGCHAIN_PROJECT']='LangchainDemo_zhang'
- os.environ['LANGCHAIN_API_KEY']='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'# 创建模型
- model = ChatOpenAI(model="gpt-3.5-turbo")# 创建返回数据的解析器
- parser = StrOutputParser()# 定义提示模板
- prompt_template = ChatPromptTemplate.from_messages([('system','你是一个非常乐于助人的助手。用{language}尽可能回答你所能回答的所有问题。'),
- MessagesPlaceholder(variable_name='my_msg')])# 得到链(把各个组件连接起来)(模板 | 模型 | 解析器)
- chain = prompt_template | model | parser
- # 保存聊天的历史记录
- store ={}# 所有用户的聊天记录都保存到sotre。key:sessionID, value: 历史聊天记录对象# 此函数预期将接收到一个session_id并返回一个消息历史记录的对象defget_session_history(session_id:str):if session_id notin store:
- store[session_id]= ChatMessageHistory()return store[session_id]
- do_message = RunnableWithMessageHistory(
- chain,
- get_session_history,
- input_messages_key='my_msg'# 每次聊天的时候发送msg的key)
- config ={'configurable':{'session_id':'zhang317'}}# 给当前会话定义一个session_id# 第一轮聊天
- resp1 = do_message.invoke({'my_msg':[HumanMessage(content="你好!我是张张。")],'language':'中文'},
- config=config
- )print(resp1)# 第二轮聊天
- resp2 = do_message.invoke({'my_msg':[HumanMessage(content='我的名字是什么?')],'language':'中文'},
- config=config
- )print(resp2)
复制代码 在输出resp1和resp2的时候有些可能用:- print(resp1.content)print(resp2.content)
复制代码 这样写是正确的,可能是因为python的版本问题。
结果:- 你好,张张!有什么问题我可以帮助你解决呢?
- 您告诉我您的名字是张张。
复制代码 现在要第三轮聊天通过流式的方式进行输出,其实就是一个token一个token的输出- config ={'configurable':{'session_id':'bobo317'}}# 第三轮聊天,返回的数据是流式的for resp in do_message.stream({'my_msg':[HumanMessage(content='请给我讲一个愚公移山的故事。')],'language':'English'},
- config=config):# 每一次resp都是一个tokenprint(resp, end='-')
复制代码 结果:- -Once- upon- a- time- in- ancient- China-,- there- was- a- man- named- Yu- Gong- who- lived- at- the- foot- of- two- large- mountains- that- blocked- his- path- and- made- it- difficult- for- his- family- to- travel-.- Determin-ed- to- make- life- easier- for- his- descendants-,- Yu- Gong- decided- to- move- the- mountains- out- of- the- way-.
- -Yu- Gong- and- his- family- began- digging- at- the- mountains- with- their- bare- hands-,- but- progress- was- slow-.- A- wise- old- man- passing- by- saw- what- they- were- doing- and- told- Yu- Gong- that- moving- the- mountains- was- an- impossible- task- for- one- family-.- Und-eter-red-,- Yu- Gong- replied-,- "-I- may- not- be- able- to- finish- this- task- in- my- lifetime-,- but- my- children- and- grandchildren- can- continue- it-.- As- long- as- we- keep- working- together-,- the- mountains- will- eventually- be- moved-."
- -Imp-ressed- by- Yu- Gong-'s- determination-,- the- wise- old- man- was- moved- by- his- spirit- and- decided- to- help-.- He- called- upon- the- gods-,- who- were- also- touched- by- Yu- Gong-'s- perseverance-.- The- gods- were- so- moved- that- they- sent- two- divine- beings- who- picked- up- the- mountains- and- carried- them- away-.
- -From- then- on-,- the- saying- "-Yu- Gong- moves- mountains-"- became- a- popular- id-iom- in- Chinese- culture-,- symbol-izing- the- power- of- determination- and- persistence- in- overcoming- seemingly- ins-ur-mount-able- obstacles-.--
复制代码 then- on-,- the- saying- “-Yu- Gong- moves- mountains-”- became- a- popular- id-iom- in- Chinese- culture-,- symbol-izing- the- power- of- determination- and- persistence- in- overcoming- seemingly- ins-ur-mount-able- obstacles-.–
原文地址:https://blog.csdn.net/m0_73665698/article/details/143645860 |