LangChain特定的默认回复

4
使用LangChain和OpenAI,我如何让模型返回特定的默认回复?例如,假设我有以下的陈述/回复。
Statement: Hi, I need to update my email address.
Answer: Thank you for updating us. Please text it here.

Statement: Hi, I have a few questions regarding my case. Can you call me back?
Answer: Hi. Yes, one of our case managers will give you a call shortly. 

如果输入与上述陈述之一相似,我希望OpenAI能够以具体答案回应。
1个回答

2
你可以通过精确的示例和AI助手的角色来处理这个问题。我在LLMChain中设置verbose = 1,这样你就可以看到观察/执行过程。
    from langchain.prompts import PromptTemplate
    from langchain.prompts import FewShotPromptTemplate

    from langchain.chat_models import ChatOpenAI
    from langchain.chains import LLMChain

    examples = [
        {
            "query": "What's the weather like?",
            "answer": "It's raining cats and dogs, better bring an umbrella!"
        },
        {
            "query": "How old are you?",
            "answer": "Age is just a number, but I'm timeless."
        },
        {
            "query":"Could you update my email address",
            "answer":"Thank you for updating us. Please text it here"
        },
        {
            "query":"I have a few questions regarding my case. Can you call me back?",
            "answer":"Yes, one of our case managers will give you a call shortly"
        }

    ]

    example_template = """ 
       User:{query},
       AI:{answer} 
    """

    example_prompt = PromptTemplate(
        input_variables=["query", "answer"],
        template=example_template
    )

    # prefix= """ The following are excerpts from conversations with an AI
    # assistant. The assistant is known for its humor and wit, providing
    # entertaining and amusing responses to users' questions. Here are some
    # examples:"""

    prefix= """ The following are excerpts from conversations with an AI
    assistant. The assistant is known for its accurate responses to users' questions. Here are some
    examples:"""

    suffix="""
    User:{query},
    AI:
    """

    few_shot_template = FewShotPromptTemplate(
        examples=examples,
        example_prompt=example_prompt,
        prefix=prefix,
        suffix=suffix,
        input_variables=["query"],
        example_separator="\n\n"
    )

    chat = ChatOpenAI(model_name="gpt-3.5-turbo-0301", temperature=0.0)
    chain = LLMChain(llm=chat, prompt=few_shot_template, verbose=1)

    print(chain.run("what's meaning of life ?"))
    print(chain.run("Could you update my email address ?"))
    print(chain.run("I have a few questions regarding my case. Can you call me back?"))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接