【LLM】LangChain基础使用(构建LLM应用)

note

  • LangChain应用开发框架,支持python和typescript语言;可以帮助生成prompt模板,并通过代理充当其他组件(如提示模板、其他大语言模型、外部数据和其他工具)的中央接口。
  • LangChain可以直接与 OpenAI 的 text-davinci-003、gpt-3.5-turbo 模型以及 Hugging Face 的各种开源语言模如 Google 的 flan-t5等模型集成。

一、LangChain介绍

  • 为各种不同基础模型提供统一接口
  • 帮助管理提示的框架
  • 一套中心化接口,用于处理长期记忆(参见Memory)、外部数据(参见Indexes)、其他 LLM(参见Chains)以及 LLM 无法处理的任务的其他代理(例如,计算或搜索)。

总的来说,有六大核心模块:

  • Models:从不同的 LLM 和嵌入模型中进行选择
  • Prompts:管理 LLM 输入
  • Chains:将 LLM 与其他组件相结合
  • Indexes:访问外部数据
  • Memory:记住以前的对话
  • Agents:访问其他工具

二、LangChain的应用

1. 集成LLM

# !/usr/bin/python
# -*- coding: utf-8 -*-
import os
from langchain.schema import HumanMessage
from langchain.chat_models import ChatOpenAI
from langchain.llms import OpenAI
from langchain import PromptTemplate
from langchain.chains import LLMChain
from langchain import PromptTemplate, FewShotPromptTemplate

os.environ["OPENAI_API_KEY"] = "..."
os.environ['HUGGINGFACEHUB_API_TOKEN'] = '...'

# # llm initialization, LangChain集成LLM模型
# pip -q install openai langchain huggingface_hub

# llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)
#
# while True:
#     human_input = input("(human): ")
#     human_input = [HumanMessage(content=human_input)]
#     ai_output = llm(human_input)
#     print(f"(ai): {ai_output.content}")

# 1. model test
llm = OpenAI(model_name='text-davinci-003',
             temperature=0.9,
             max_tokens=1024)

text = "老鼠生病了能吃老鼠药吗?"
print(llm(text)) 
# 不能。老鼠药只能用于驱除和杀死老鼠,不能用于治疗老鼠疾病。如果老鼠生病了,需要求助专业兽医进行治疗。

2. Prompts创建

# 2. prompt generation
restaurant_template = """
我想让你成为一个给新开餐馆命名的顾问。
给我返回一个餐馆名字的名单. 每个餐馆名字要简单, 朗朗上口且容易记住. 它应该和你命名的餐馆类型有关.
关于{restaurant_desription} 这家餐馆好听名字有哪些?
"""
# 创建一个prompt模板
prompt_template = PromptTemplate(
    input_variables=["restaurant_desription"],
    template=restaurant_template
)
description = "一家以婚纱摄影为主题的汉堡店"
description_02 = "一家拉面店,营业员都穿着汉服"
description_03 = "一家能看到海景的烤肉店"
# 查看模板生成的生成的内容。
print(prompt_template.format(restaurant_desription=description))
# 应用prompt模板
chain = LLMChain(llm = llm, prompt = prompt_template)
print(chain.run("一家以婚纱摄影为主题的汉堡店"))

1. 海洋蔚蓝婚纱汉堡
2. 白马婚礼汉堡
3. 幸福煎饼汉堡
4. 唯美烤肉汉堡
5. 爱情芝士汉堡
6. 天使之翼汉堡
7. 星期五婚礼汉堡
8. 情侣奇妙汉堡
9. 时尚恋人汉堡
10. 魅力婚礼汉堡

3. 短语模板

# 3. 短语模板
# 首先创建一个短语示例,该示例包含两组输入和输出,每输入一个词语,LLM就会输出一个对应的反义词
examples = [
    {
    
    "输入": "高兴", "输出": "悲伤"},
    {
    
    "输入": "高大", "输出": "低矮"},
]
#创建一个prompt模板,
example_prompt = PromptTemplate(
    input_variables=["输入", "输出"],
    template="\n输入: {输入}\n输出: {输出}\n",
)
# 最后我们创建一个短语prompt模板对象
few_shot_prompt = FewShotPromptTemplate(
    # 这些是我们要插入到prompt中的示例
    examples=examples,
    # 将示例插入prompt时,格式化示例的方式。
    example_prompt=example_prompt,
    # 输入变量是用户直接输入的变量
    input_variables=["input"],
    # 前缀变量
    prefix="给出每个输入词语的反义词",
    # 后缀变量
    suffix="输入: {input}\n输出:",
    # 用来连接前缀、示例和后缀的字符串。
    example_separator="\n",
)

# 测试一下短语模板对象
print(few_shot_prompt.format(input="快乐"))

# 在LLM中应用短语模板
from langchain.chains import LLMChain
chain = LLMChain(llm=llm, prompt=few_shot_prompt)

chain.run("善良")
Out[14]: ' 恶毒'
chain.run("肥仔")
Out[15]: ' 瘦子'

在这里插入图片描述

Reference

[1] 用LangChain构建大语言模型应用
[2] https://blog.langchain.dev/announcing-our-10m-seed-round-led-by-benchmark/
[3] Langchain官方文档:https://python.langchain.com/en/latest/
[4] LangChain与大型语言模型(LLMs)应用基础教程:Prompt模板

猜你喜欢

转载自blog.csdn.net/qq_35812205/article/details/130630860