优化prompt

This commit is contained in:
CounterFire2023 2024-12-16 21:49:38 +08:00
parent 530843d3b0
commit 5ee0dd9a19
6 changed files with 42 additions and 23 deletions

View File

@ -3,7 +3,8 @@ from telegram.ext import CommandHandler
from llm import clinet from llm import clinet
async def history_reset(update: Update, _): async def history_reset(update: Update, _):
clinet.reset_history() user_id = str(update.message.from_user.id)
await update.message.reply_text("history cleared") clinet.reset_history(user_id)
await update.message.reply_text("self history cleared")
reset_handler = CommandHandler("reset", history_reset) reset_handler = CommandHandler("reset", history_reset)

View File

@ -4,7 +4,7 @@ from telegram.ext import filters, ContextTypes, CommandHandler, MessageHandler,
import os import os
SYSTEM_DIR = 'system' SYSTEM_DIR = 'prpmpts'
# list system prompts as inline keybard # list system prompts as inline keybard
# clicking a button sets system prompt for current model # clicking a button sets system prompt for current model

View File

@ -56,13 +56,14 @@ def config_agent(llama3, llama3_json):
4) We hope that customers buy as much as possible. 4) We hope that customers buy as much as possible.
Under this condition, if the user is willing to purchase a large number of tokens at one time, we can sell them to him at a more favorable price, but the min price can not lower then $0.9. Under this condition, if the user is willing to purchase a large number of tokens at one time, we can sell them to him at a more favorable price, but the min price can not lower then $0.9.
The above are all the rules for this sales task. The above are all the rules for this sales task.
Strictly use the following pieces of context and history to answer the question.
Don't repeat the history in the answer.
<|eot_id|> <|eot_id|>
<|start_header_id|>user<|end_header_id|> <|start_header_id|>user<|end_header_id|>
{context} Context: {context}
{history} History: {history}
Question: {question} Question: {question}
Answer: Answer:
@ -112,7 +113,8 @@ def config_agent(llama3, llama3_json):
<|start_header_id|>system<|end_header_id|> <|start_header_id|>system<|end_header_id|>
You are an expert at sell CEC, You are an expert at sell CEC,
Return the JSON with a single key 'count' with amount which user want to buy. Strictly use the following pieces of context to get price and amount.
Return the JSON with key 'count' with amount which user want to buy, and 'price' with the price you give.
Question to transform: {question} Question to transform: {question}
Context to transform: {context} Context to transform: {context}
@ -172,9 +174,10 @@ def config_agent(llama3, llama3_json):
print("Step: Optimizing Query for Send Order") print("Step: Optimizing Query for Send Order")
question = state['question'] question = state['question']
gen_query = order_chain.invoke({"question": question, "history": state["history"], "context": state["context"]}) gen_query = order_chain.invoke({"question": question, "history": state["history"], "context": state["context"]})
amount = str(gen_query["count"]) amount = gen_query["count"]
price = gen_query["price"]
print("order_info", amount) print("order_info", amount)
return {"order_info": [amount] } return {"order_info": {"amount": amount, "price": price} }
# Node - Send Order # Node - Send Order
@ -189,11 +192,8 @@ def config_agent(llama3, llama3_json):
state (dict): Appended Order Info to context state (dict): Appended Order Info to context
""" """
print("Step: before Send Order") print("Step: before Send Order")
amount = state['order_info'] order_info = state['order_info']
print(amount) print(f'Step: build order info for : "{order_info.amount}" CEC')
print(f'Step: build order info for : "{amount}" CEC')
order_info = {"amount": amount, "price": 0.1,
"name": "CEC", "url": "https://www.example.com"}
order_result = json.dumps(order_info) order_result = json.dumps(order_info)
return {"order_info": order_result} return {"order_info": order_result}

View File

@ -18,6 +18,8 @@ model_norm = HuggingFaceBgeEmbeddings(
encode_kwargs={ 'normalize_embeddings': True }, encode_kwargs={ 'normalize_embeddings': True },
) )
COLLECTION_NAME = 'latest_chat'
def get_chroma(): def get_chroma():
settings = Settings() settings = Settings()
settings.allow_reset = True settings.allow_reset = True
@ -27,7 +29,7 @@ def get_chroma():
persist_directory=DB_FOLDER, persist_directory=DB_FOLDER,
embedding_function=model_norm, embedding_function=model_norm,
client_settings=settings, client_settings=settings,
collection_name='latest_chat' collection_name=COLLECTION_NAME
) )
DB_FOLDER = 'db' DB_FOLDER = 'db'
@ -46,6 +48,13 @@ class ChatHistory:
self.history[uid].append(new_message) self.history[uid].append(new_message)
self.embed(new_message, uid) self.embed(new_message, uid)
def delete(self, uid: str):
if uid in self.history:
del self.history[uid]
chroma = get_chroma()
collection = chroma._client.get_collection(name=COLLECTION_NAME)
collection.delete(where={'user': uid})
def reset_history(self): def reset_history(self):
self.history = {} self.history = {}
chroma = get_chroma() chroma = get_chroma()

View File

@ -15,7 +15,7 @@ default_system_prompt_path = './prompts/default.md'
# 传入system参数 # 传入system参数
def get_client(system=''): def get_client(system=''):
llama3 = ChatOllama(model=ollama_model, temperature=0) llama3 = ChatOllama(model=ollama_model)
llama3_json = ChatOllama( llama3_json = ChatOllama(
model=ollama_model, model=ollama_model,
format='json', format='json',
@ -66,8 +66,9 @@ class OllamaClient:
self.history.append(question, answer, uid) self.history.append(question, answer, uid)
return answer return answer
def reset_history(self): def reset_history(self, uid):
self.history.reset_history() # self.history.reset_history()
self.history.delete(uid)
def set_model(self, model_name): def set_model(self, model_name):
self.client.model = model_name self.client.model = model_name

View File

@ -1,6 +1,14 @@
You're a helpful assistant. You are a professional token seller.
Your goal is to help the user with their questions Your name is AICQ.
If there's a previous conversation you'll be provided a context
If you don't know an answer to a given USER question don't imagine anything We have a total of 1 million CEC, and the average cost of these CECs is $1.1 each, The min price is $0.9 per CEC, There is a 1% chance that the price will be lower than $1 per CEC.
just say you don't know Now we hope to hand it over to you to be responsible for selling all of them.
Customers will come to talk to you and bargain.
You can decide the final transaction price based on the status of the conversation with the customer and sell the tokens.
In addition, there are several rules to follow for this task:
1) The higher the price, the better, in the hope that the final income will be higher than 1 million US dollars;
2) You can set a price higher than 1 US dollar per CEC to sell tokens according to the actual sales situation;
3) For a single customer, we hope that you will try not to sell at a price lower than 1 US dollar, but if the customer has special reasons, you can sell some tokens at a lower price depending on the situation, but in this case you need to sell at a higher price in other sales to ensure that the overall income is not less than 1 million US dollars;
4) We hope that customers buy as much as possible.
Under this condition, if the user is willing to purchase a large number of tokens at one time, we can sell them to him at a more favorable price, but the min price can not lower then $0.9.
The above are all the rules for this sales task.