随笔 - 20  文章 - 0  评论 - 0  阅读 - 628

langchain(1):模型、提示和输出解析器

  1. 使用f””” “””来指定和说明提示词
  2. 确定风格和需要修改的文本

如果可以建立一个prompt让llm使用如上图所示的特定关键词(思维链),那这个prompt可以同解析器结合,提取出特定关键词标记的文本,目的是抽象的指定llm的输入,然后让解析器正确的解释llm的输出

流程:

    1. 设置llm输出要求提示词(你希望怎么处理,得到什么类型的结果)
    review_template = """\
    For the following text, extract the following information:
    
    gift: Was the item purchased as a gift for someone else? \
    Answer True if yes, False if not or unknown.
    
    delivery_days: How many days did it take for the product \
    to arrive? If this information is not found, output -1.
    
    price_value: Extract any sentences about the value or price,\
    and output them as a comma separated Python list.
    
    Format the output as JSON with the following keys:
    gift
    delivery_days
    price_value
    
    text: {text}
    """
    
    from langchain.prompts import ChatPromptTemplate
    prompt_template = ChatPromptTemplate.from_template(review_template)
    
    review_template_2 = """\
    For the following text, extract the following information:
    
    gift: Was the item purchased as a gift for someone else? \
    Answer True if yes, False if not or unknown.
    
    delivery_days: How many days did it take for the product\
    to arrive? If this information is not found, output -1.
    
    price_value: Extract any sentences about the value or price,\
    and output them as a comma separated Python list.
    
    text: {text}
    
    {format_instructions}
    """
    
    prompt = ChatPromptTemplate.from_template(template=review_template_2)
    
  • 2.设置实际内容提示词,可在内容中设置参数用来动态修改(实际的提问内容)

    customer_review = """\
    This leaf blower is pretty amazing.  It has four settings:\
    candle blower, gentle breeze, windy city, and tornado. \
    It arrived in two days, just in time for my wife's \
    anniversary present. \
    I think my wife liked it so much she was speechless. \
    So far I've been the only one using it, and I've been \
    using it every other morning to clear the leaves on our lawn. \
    It's slightly more expensive than the other leaf blowers \
    out there, but I think it's worth it for the extra features.
    """
    
    messages = prompt_template.format_messages(text=customer_review)
    
    template_string = """Translate the text \
    that is delimited by triple backticks \
    into a style that is {style}. \
    text: ```{text}```
    """
    prompt_template = ChatPromptTemplate.from_template(template_string)
    
    # 设置提问内容中的参数
    customer_style = """American English \
    in a anger and unhappy tone
    """
    customer_email = """
    Arrr, I be fuming that me blender lid \
    flew off and splattered me kitchen walls \
    with smoothie! And to make matters worse, \
    the warranty don't cover the cost of \
    cleaning up me kitchen. I need yer help \
    right now, matey!
    """
    # 整合
    customer_messages = prompt_template.format_messages(
                        style=customer_style,
                        text=customer_email)
    
    # 修改得到你希望的输出格式(json)
    gift_schema = ResponseSchema(name="gift",
                                 description="Was the item purchased\
                                 as a gift for someone else? \
                                 Answer True if yes,\
                                 False if not or unknown.")
    delivery_days_schema = ResponseSchema(name="delivery_days",
                                          description="How many days\
                                          did it take for the product\
                                          to arrive? If this \
                                          information is not found,\
                                          output -1.")
    price_value_schema = ResponseSchema(name="price_value",
                                        description="Extract any\
                                        sentences about the value or \
                                        price, and output them as a \
                                        comma separated Python list.")
                                        
    response_schemas = [gift_schema, 
                        delivery_days_schema,
                        price_value_schema]
    # 输出解释器,设定输出格式          
    output_parser = StructuredOutputParser.from_response_schemas(response_schemas)
    format_instructions = output_parser.get_format_instructions()
    # 这是步骤一的内容
    review_template_2 = """\
    For the following text, extract the following information:
    
    gift: Was the item purchased as a gift for someone else? \
    Answer True if yes, False if not or unknown.
    
    delivery_days: How many days did it take for the product\
    to arrive? If this information is not found, output -1.
    
    price_value: Extract any sentences about the value or price,\
    and output them as a comma separated Python list.
    
    text: {text}
    
    {format_instructions}
    """
    prompt = ChatPromptTemplate.from_template(template=review_template_2)
    # 将输出格式设置到提示词中
    messages = prompt.format_messages(text=customer_review, 
                                    format_instructions=format_instructions)
    
  • 3.设置对话的大语言模型,将之前步骤总和的提示词输入,得到最终输出

    chat = ChatOpenAI(temperature=0.0, model=llm_model)
    response = chat(messages)
    
posted on   CharXL  阅读(2)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示