mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-16 15:04:13 +00:00
Templates (#12294)
Co-authored-by: Harrison Chase <hw.chase.17@gmail.com> Co-authored-by: Lance Martin <lance@langchain.dev> Co-authored-by: Jacob Lee <jacoblee93@gmail.com>
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
from operator import itemgetter
|
||||
|
||||
from langchain.prompts import ChatPromptTemplate
|
||||
from langchain.chat_models import ChatOpenAI
|
||||
from langchain.schema.output_parser import StrOutputParser
|
||||
from langchain.schema.runnable import RunnablePassthrough, RunnableLambda
|
||||
from langchain.utilities import DuckDuckGoSearchAPIWrapper
|
||||
|
||||
template = """Answer the users question based only on the following context:
|
||||
|
||||
<context>
|
||||
{context}
|
||||
</context>
|
||||
|
||||
Question: {question}
|
||||
"""
|
||||
prompt = ChatPromptTemplate.from_template(template)
|
||||
|
||||
model = ChatOpenAI(temperature=0)
|
||||
|
||||
search = DuckDuckGoSearchAPIWrapper()
|
||||
|
||||
|
||||
def retriever(query):
|
||||
return search.run(query)
|
||||
|
||||
template = """Provide a better search query for \
|
||||
web search engine to answer the given question, end \
|
||||
the queries with ’**’. Question: \
|
||||
{x} Answer:"""
|
||||
rewrite_prompt = ChatPromptTemplate.from_template(template)
|
||||
|
||||
# Parser to remove the `**`
|
||||
|
||||
def _parse(text):
|
||||
return text.strip("**")
|
||||
|
||||
rewriter = rewrite_prompt | ChatOpenAI(temperature=0) | StrOutputParser() | _parse
|
||||
|
||||
chain = {
|
||||
"context": {"x": RunnablePassthrough()} | rewriter | retriever,
|
||||
"question": RunnablePassthrough()
|
||||
} | prompt | model | StrOutputParser()
|
Reference in New Issue
Block a user