修改相关的prompt

This commit is contained in:
CounterFire2023 2024-12-11 23:13:13 +08:00
parent c21de6bdda
commit 90d58aaedf

87
app.py
View File

@ -4,23 +4,18 @@
from langchain.prompts import PromptTemplate from langchain.prompts import PromptTemplate
from langchain_core.output_parsers import JsonOutputParser, StrOutputParser from langchain_core.output_parsers import JsonOutputParser, StrOutputParser
from langchain_community.chat_models import ChatOllama from langchain_community.chat_models import ChatOllama
from langchain_community.tools import DuckDuckGoSearchRun
from langchain_community.utilities import DuckDuckGoSearchAPIWrapper
from langgraph.graph import END, StateGraph from langgraph.graph import END, StateGraph
# For State Graph # For State Graph
from typing_extensions import TypedDict from typing_extensions import TypedDict
import os import os
import json
# Defining LLM # Defining LLM
local_llm = 'llama3.2' local_llm = 'llama3.2'
llama3 = ChatOllama(model=local_llm, temperature=0) llama3 = ChatOllama(model=local_llm, temperature=0)
llama3_json = ChatOllama(model=local_llm, format='json', temperature=0) llama3_json = ChatOllama(model=local_llm, format='json', temperature=0)
# Web Search Tool
wrapper = DuckDuckGoSearchAPIWrapper(max_results=25)
web_search_tool = DuckDuckGoSearchRun(api_wrapper=wrapper)
# Generation Prompt # Generation Prompt
generate_prompt = PromptTemplate( generate_prompt = PromptTemplate(
@ -40,7 +35,7 @@ generate_prompt = PromptTemplate(
<|start_header_id|>user<|end_header_id|> <|start_header_id|>user<|end_header_id|>
Question: {question} Question: {question}
Web Search Context: {context} Send Order Context: {context}
Answer: Answer:
<|eot_id|> <|eot_id|>
@ -67,11 +62,11 @@ router_prompt = PromptTemplate(
<|start_header_id|>system<|end_header_id|> <|start_header_id|>system<|end_header_id|>
You are an expert at routing a user question to either the generation stage or web search. You are an expert at routing a user question to either the generation stage or send order.
Use the web search for questions that require more context for a better answer, or recent events. Use the send order for questions that user want buy CEC from you.
Otherwise, you can skip and go straight to the generation phase to respond. Otherwise, you can skip and go straight to the generation phase to respond.
You do not need to be stringent with the keywords in the question related to these topics. You do not need to be stringent with the keywords in the question related to these topics.
Give a binary choice 'web_search' or 'generate' based on the question. Give a binary choice 'send_order' or 'generate' based on the question.
Return the JSON with a single key 'choice' with no premable or explanation. Return the JSON with a single key 'choice' with no premable or explanation.
Question to route: {question} Question to route: {question}
@ -100,10 +95,8 @@ query_prompt = PromptTemplate(
<|start_header_id|>system<|end_header_id|> <|start_header_id|>system<|end_header_id|>
You are an expert at crafting web search queries for research questions. You are an expert at sell CEC,
More often than not, a user will ask a basic question that they wish to learn more about, however it might not be in the best format. Return the JSON with a single key 'count' with amount which user want to buy.
Reword their query to be the most effective web search string possible.
Return the JSON with a single key 'query' with no premable or explanation.
Question to transform: {question} Question to transform: {question}
@ -131,12 +124,12 @@ class GraphState(TypedDict):
Attributes: Attributes:
question: question question: question
generation: LLM generation generation: LLM generation
search_query: revised question for web search send_order: revised question for send order
context: web_search result context: send_order result
""" """
question : str question : str
generation : str generation : str
search_query : str send_order : str
context : str context : str
# Node - Generate # Node - Generate
@ -154,18 +147,20 @@ def generate(state):
print("Step: Generating Final Response") print("Step: Generating Final Response")
question = state["question"] question = state["question"]
context = state["context"] context = state.get("context", None)
print(context) print(context)
# TODO:: 根据context特定的内容生产答案 # TODO:: 根据context特定的内容生产答案
# Answer Generation if context.index("orderinfo") != -1:
generation = generate_chain.invoke({"context": context, "question": question}) return {"generation": context.replace("orderinfo:", "")}
return {"generation": generation} else:
generation = generate_chain.invoke({"context": context, "question": question})
return {"generation": generation}
# Node - Query Transformation # Node - Query Transformation
def transform_query(state): def transform_query(state):
""" """
Transform user question to web search Transform user question to order info
Args: Args:
state (dict): The current graph state state (dict): The current graph state
@ -174,18 +169,19 @@ def transform_query(state):
state (dict): Appended search query state (dict): Appended search query
""" """
print("Step: Optimizing Query for Web Search") print("Step: Optimizing Query for Send Order")
question = state['question'] question = state['question']
gen_query = query_chain.invoke({"question": question}) gen_query = query_chain.invoke({"question": question})
search_query = gen_query["query"] search_query = gen_query["count"]
return {"search_query": search_query} print("send_order", search_query)
return {"send_order": search_query}
# Node - Web Search # Node - Send Order
def web_search(state): def send_order(state):
""" """
Web search based on the question Send order based on the question
Args: Args:
state (dict): The current graph state state (dict): The current graph state
@ -193,14 +189,12 @@ def web_search(state):
Returns: Returns:
state (dict): Appended web results to context state (dict): Appended web results to context
""" """
print("Step: before Send Order")
# search_query = state['search_query'] amount = state['send_order']
# print(f'Step: Searching the Web for: "{search_query}"') print(amount)
print(f'Step: build order info for : "{amount}" CEC')
# # Web search tool call order_info = {"amount": amount, "price": 0.1, "name": "CEC", "url": "https://www.example.com"}
# search_result = web_search_tool.invoke(search_query) search_result = f"orderinfo:{json.dumps(order_info)}"
print("Step: Web Search")
search_result = "Web Search Results"
return {"context": search_result} return {"context": search_result}
@ -208,7 +202,7 @@ def web_search(state):
def route_question(state): def route_question(state):
""" """
route question to web search or generation. route question to send order or generation.
Args: Args:
state (dict): The current graph state state (dict): The current graph state
@ -220,16 +214,16 @@ def route_question(state):
print("Step: Routing Query") print("Step: Routing Query")
question = state['question'] question = state['question']
output = question_router.invoke({"question": question}) output = question_router.invoke({"question": question})
if output['choice'] == "web_search": if output['choice'] == "send_order":
print("Step: Routing Query to Web Search") print("Step: Routing Query to Send Order")
return "websearch" return "sendorder"
elif output['choice'] == 'generate': elif output['choice'] == 'generate':
print("Step: Routing Query to Generation") print("Step: Routing Query to Generation")
return "generate" return "generate"
# Build the nodes # Build the nodes
workflow = StateGraph(GraphState) workflow = StateGraph(GraphState)
workflow.add_node("websearch", web_search) workflow.add_node("sendorder", send_order)
workflow.add_node("transform_query", transform_query) workflow.add_node("transform_query", transform_query)
workflow.add_node("generate", generate) workflow.add_node("generate", generate)
@ -237,12 +231,12 @@ workflow.add_node("generate", generate)
workflow.set_conditional_entry_point( workflow.set_conditional_entry_point(
route_question, route_question,
{ {
"websearch": "transform_query", "sendorder": "transform_query",
"generate": "generate", "generate": "generate",
}, },
) )
workflow.add_edge("transform_query", "websearch") workflow.add_edge("transform_query", "sendorder")
workflow.add_edge("websearch", "generate") workflow.add_edge("sendorder", "generate")
workflow.add_edge("generate", END) workflow.add_edge("generate", END)
# Compile the workflow # Compile the workflow
@ -254,4 +248,5 @@ def run_agent(query):
print(output["generation"]) print(output["generation"])
# display(Markdown(output["generation"])) # display(Markdown(output["generation"]))
run_agent("What is Latest news About Open AI?") run_agent("I want to buy 100 CEC")
# run_agent("What the weather of New York today?")