修改相关的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_core.output_parsers import JsonOutputParser, StrOutputParser
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
# For State Graph
from typing_extensions import TypedDict
import os
import json
# Defining LLM
local_llm = 'llama3.2'
llama3 = ChatOllama(model=local_llm, 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
generate_prompt = PromptTemplate(
@ -40,7 +35,7 @@ generate_prompt = PromptTemplate(
<|start_header_id|>user<|end_header_id|>
Question: {question}
Web Search Context: {context}
Send Order Context: {context}
Answer:
<|eot_id|>
@ -67,11 +62,11 @@ router_prompt = PromptTemplate(
<|start_header_id|>system<|end_header_id|>
You are an expert at routing a user question to either the generation stage or web search.
Use the web search for questions that require more context for a better answer, or recent events.
You are an expert at routing a user question to either the generation stage or send order.
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.
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.
Question to route: {question}
@ -100,10 +95,8 @@ query_prompt = PromptTemplate(
<|start_header_id|>system<|end_header_id|>
You are an expert at crafting web search queries for research questions.
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.
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.
You are an expert at sell CEC,
Return the JSON with a single key 'count' with amount which user want to buy.
Question to transform: {question}
@ -131,12 +124,12 @@ class GraphState(TypedDict):
Attributes:
question: question
generation: LLM generation
search_query: revised question for web search
context: web_search result
send_order: revised question for send order
context: send_order result
"""
question : str
generation : str
search_query : str
send_order : str
context : str
# Node - Generate
@ -154,18 +147,20 @@ def generate(state):
print("Step: Generating Final Response")
question = state["question"]
context = state["context"]
context = state.get("context", None)
print(context)
# TODO:: 根据context特定的内容生产答案
# Answer Generation
generation = generate_chain.invoke({"context": context, "question": question})
return {"generation": generation}
if context.index("orderinfo") != -1:
return {"generation": context.replace("orderinfo:", "")}
else:
generation = generate_chain.invoke({"context": context, "question": question})
return {"generation": generation}
# Node - Query Transformation
def transform_query(state):
"""
Transform user question to web search
Transform user question to order info
Args:
state (dict): The current graph state
@ -174,18 +169,19 @@ def transform_query(state):
state (dict): Appended search query
"""
print("Step: Optimizing Query for Web Search")
print("Step: Optimizing Query for Send Order")
question = state['question']
gen_query = query_chain.invoke({"question": question})
search_query = gen_query["query"]
return {"search_query": search_query}
search_query = gen_query["count"]
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:
state (dict): The current graph state
@ -193,14 +189,12 @@ def web_search(state):
Returns:
state (dict): Appended web results to context
"""
# search_query = state['search_query']
# print(f'Step: Searching the Web for: "{search_query}"')
# # Web search tool call
# search_result = web_search_tool.invoke(search_query)
print("Step: Web Search")
search_result = "Web Search Results"
print("Step: before Send Order")
amount = state['send_order']
print(amount)
print(f'Step: build order info for : "{amount}" CEC')
order_info = {"amount": amount, "price": 0.1, "name": "CEC", "url": "https://www.example.com"}
search_result = f"orderinfo:{json.dumps(order_info)}"
return {"context": search_result}
@ -208,7 +202,7 @@ def web_search(state):
def route_question(state):
"""
route question to web search or generation.
route question to send order or generation.
Args:
state (dict): The current graph state
@ -220,16 +214,16 @@ def route_question(state):
print("Step: Routing Query")
question = state['question']
output = question_router.invoke({"question": question})
if output['choice'] == "web_search":
print("Step: Routing Query to Web Search")
return "websearch"
if output['choice'] == "send_order":
print("Step: Routing Query to Send Order")
return "sendorder"
elif output['choice'] == 'generate':
print("Step: Routing Query to Generation")
return "generate"
# Build the nodes
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("generate", generate)
@ -237,12 +231,12 @@ workflow.add_node("generate", generate)
workflow.set_conditional_entry_point(
route_question,
{
"websearch": "transform_query",
"sendorder": "transform_query",
"generate": "generate",
},
)
workflow.add_edge("transform_query", "websearch")
workflow.add_edge("websearch", "generate")
workflow.add_edge("transform_query", "sendorder")
workflow.add_edge("sendorder", "generate")
workflow.add_edge("generate", END)
# Compile the workflow
@ -254,4 +248,5 @@ def run_agent(query):
print(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?")