开启左侧

Langchain的安装

[复制链接]
gnxhxbozd 发表于 10 小时前 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题
作者:CSDN博客
Langchain

1、python中安装langchain
  1. pip install langchain
  2. pip install langchain-openai
复制代码
​        登录官网,获取LangSmish的API key
​        https://smith.langchain.com/settings
2、案例一:创建一个简单的调用大模型的案例:
  1. import os
  2. from langchain_core.messages import SystemMessage, HumanMessage
  3. from langchain_core.output_parsers import StrOutputParser
  4. from langchain_openai import ChatOpenAI
  5. os.environ['http_proxy']=''
  6. os.environ['https_proxy']=''
  7. os.environ['LANGCHAIN_TRACING_V2']='TRUE'
  8. os.environ['LANGCHAIN_API_KEY']=''# 创建模型
  9. model = ChatOpenAI(model="gpt-3.5-turbo-1106")# 准备提示prompt
  10. msg =[
  11.     SystemMessage(content="请将以下内容翻译成日语"),
  12.     HumanMessage(content="你好,请问你要到哪里去?")]
  13. result = model.invoke(msg)print(result)# 创建返回数据的解析器
  14. parser = StrOutputParser()
  15. return_str = parser.invoke(result)print(return_str)# 得到链(把各个组件连接起来)
  16. chain = model | parser
  17. # 直接使用chain来调用print(chain.invoke(msg))
复制代码
结果:
  1. 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}}
  2. こんにちは、どちらに行かれる予定ですか?
  3. こんにちは、どこに行く予定ですか?
复制代码
构建一个简单的大语言模型(LLM)应用程序
    调用语言模型
    使用OutPutParsers:输出解析器
  • 使用PromptTemplate:提示模板
    1. # 定义提示模板
    2. prompt_template = ChatPromptTemplate.from_messages([('system','请将下面的内容翻译成{language}'),('user','{text}')])# 得到链(把各个组件连接起来)(模板 | 模型 | 解析器)
    3. chain = prompt_template | model | parser
    4. # 直接使用chain来调用print(chain.invoke({'language':'English','text':'白日依山尽,黄河入海流。'}))
    复制代码
    使用LangSmish追踪你的应用程序
  • 使用LangServe部署你的应用程序
    1. pip install "langserve[all]"
    复制代码
    1. # 把我们的程序部署成服务# 创建fastAPI的应用
    2. app = FastAPI(title="my LangChain serve", version="V1.0", description="translate any language")# 添加路由
    3. add_routes(
    4.     app,
    5.     chain,
    6.     path="/chain_demo",)if __name__ =='__main__':import uvicorn
    7.     uvicorn.run(app, host='127.0.0.1', port=8000)
    复制代码
    Langchain的安装-1.jpg


​                通过代码连接服务器,实现对程序的应用
  1. from langserve import RemoteRunnable
  2. if __name__ =='__main__':
  3.     client = RemoteRunnable("http://127.0.0.1:8000/chain_demo")
  4.     result = client.invoke({'language':"italian",'text':"你好!"})print(result)
复制代码
​                结果:
  1. Ciao!
复制代码
3、案例二:Langchain构建聊天机器人

聊天机器人能够进行对话,并记住之前的互动
安装:
  1. pip install langchain_community
复制代码
机器人根据上下文来回答问题:
  1. import os
  2. from langchain_community.chat_message_histories import ChatMessageHistory
  3. from langchain_core.messages import HumanMessage
  4. from langchain_core.output_parsers import StrOutputParser
  5. from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
  6. from langchain_core.runnables import RunnableWithMessageHistory
  7. from langchain_openai import ChatOpenAI
  8. # vpn
  9. os.environ['http_proxy']='127.0.0.1:7890'
  10. os.environ['https_proxy']='127.0.0.1:7890'# 连接langchain
  11. os.environ['LANGCHAIN_TRACING_V2']='TRUE'
  12. os.environ['LANGCHAIN_PROJECT']='LangchainDemo_zhang'
  13. os.environ['LANGCHAIN_API_KEY']='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'# 创建模型
  14. model = ChatOpenAI(model="gpt-3.5-turbo")# 创建返回数据的解析器
  15. parser = StrOutputParser()# 定义提示模板
  16. prompt_template = ChatPromptTemplate.from_messages([('system','你是一个非常乐于助人的助手。用{language}尽可能回答你所能回答的所有问题。'),
  17.     MessagesPlaceholder(variable_name='my_msg')])# 得到链(把各个组件连接起来)(模板 | 模型 | 解析器)
  18. chain = prompt_template | model | parser
  19. # 保存聊天的历史记录
  20. store ={}# 所有用户的聊天记录都保存到sotre。key:sessionID, value: 历史聊天记录对象# 此函数预期将接收到一个session_id并返回一个消息历史记录的对象defget_session_history(session_id:str):if session_id notin store:
  21.         store[session_id]= ChatMessageHistory()return store[session_id]
  22. do_message = RunnableWithMessageHistory(
  23.     chain,
  24.     get_session_history,
  25.     input_messages_key='my_msg'# 每次聊天的时候发送msg的key)
  26. config ={'configurable':{'session_id':'zhang317'}}# 给当前会话定义一个session_id# 第一轮聊天
  27. resp1 = do_message.invoke({'my_msg':[HumanMessage(content="你好!我是张张。")],'language':'中文'},
  28.     config=config
  29. )print(resp1)# 第二轮聊天
  30. resp2 = do_message.invoke({'my_msg':[HumanMessage(content='我的名字是什么?')],'language':'中文'},
  31.     config=config
  32. )print(resp2)
复制代码
在输出resp1和resp2的时候有些可能用:
  1. print(resp1.content)print(resp2.content)
复制代码
这样写是正确的,可能是因为python的版本问题。
结果:
  1. 你好,张张!有什么问题我可以帮助你解决呢?
  2. 您告诉我您的名字是张张。
复制代码
现在要第三轮聊天通过流式的方式进行输出,其实就是一个token一个token的输出
  1. config ={'configurable':{'session_id':'bobo317'}}# 第三轮聊天,返回的数据是流式的for resp in do_message.stream({'my_msg':[HumanMessage(content='请给我讲一个愚公移山的故事。')],'language':'English'},
  2.                               config=config):# 每一次resp都是一个tokenprint(resp, end='-')
复制代码
结果:
  1. -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-.
  2. -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-."
  3. -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-.
  4. -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
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

发布主题
阅读排行更多+

Powered by Discuz! X3.4© 2001-2013 Discuz Team.( 京ICP备17022993号-3 )