diff --git a/handlers/history.py b/handlers/history.py index 9eb0f03..193ed1e 100644 --- a/handlers/history.py +++ b/handlers/history.py @@ -3,7 +3,8 @@ from telegram.ext import CommandHandler from llm import clinet async def history_reset(update: Update, _): - clinet.reset_history() - await update.message.reply_text("history cleared") + user_id = str(update.message.from_user.id) + clinet.reset_history(user_id) + await update.message.reply_text("self history cleared") reset_handler = CommandHandler("reset", history_reset) \ No newline at end of file diff --git a/handlers/system.py b/handlers/system.py index f222c05..5713800 100644 --- a/handlers/system.py +++ b/handlers/system.py @@ -4,7 +4,7 @@ from telegram.ext import filters, ContextTypes, CommandHandler, MessageHandler, import os -SYSTEM_DIR = 'system' +SYSTEM_DIR = 'prpmpts' # list system prompts as inline keybard # clicking a button sets system prompt for current model diff --git a/llm/agent_util.py b/llm/agent_util.py index ca79297..03793de 100644 --- a/llm/agent_util.py +++ b/llm/agent_util.py @@ -56,13 +56,14 @@ def config_agent(llama3, llama3_json): 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. - + Strictly use the following pieces of context and history to answer the question. + Don't repeat the history in the answer. <|eot_id|> <|start_header_id|>user<|end_header_id|> - {context} - {history} + Context: {context} + History: {history} Question: {question} Answer: @@ -112,7 +113,8 @@ def config_agent(llama3, llama3_json): <|start_header_id|>system<|end_header_id|> 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} Context to transform: {context} @@ -172,9 +174,10 @@ def config_agent(llama3, llama3_json): print("Step: Optimizing Query for Send Order") question = state['question'] 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) - return {"order_info": [amount] } + return {"order_info": {"amount": amount, "price": price} } # Node - Send Order @@ -189,11 +192,8 @@ def config_agent(llama3, llama3_json): state (dict): Appended Order Info to context """ print("Step: before Send Order") - amount = state['order_info'] - 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"} + order_info = state['order_info'] + print(f'Step: build order info for : "{order_info.amount}" CEC') order_result = json.dumps(order_info) return {"order_info": order_result} diff --git a/llm/chat_history.py b/llm/chat_history.py index 362db02..b0b52ef 100644 --- a/llm/chat_history.py +++ b/llm/chat_history.py @@ -18,6 +18,8 @@ model_norm = HuggingFaceBgeEmbeddings( encode_kwargs={ 'normalize_embeddings': True }, ) +COLLECTION_NAME = 'latest_chat' + def get_chroma(): settings = Settings() settings.allow_reset = True @@ -27,7 +29,7 @@ def get_chroma(): persist_directory=DB_FOLDER, embedding_function=model_norm, client_settings=settings, - collection_name='latest_chat' + collection_name=COLLECTION_NAME ) DB_FOLDER = 'db' @@ -46,6 +48,13 @@ class ChatHistory: self.history[uid].append(new_message) 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): self.history = {} chroma = get_chroma() diff --git a/llm/ollama.py b/llm/ollama.py index b638d08..ce91d04 100644 --- a/llm/ollama.py +++ b/llm/ollama.py @@ -15,7 +15,7 @@ default_system_prompt_path = './prompts/default.md' # 传入system参数 def get_client(system=''): - llama3 = ChatOllama(model=ollama_model, temperature=0) + llama3 = ChatOllama(model=ollama_model) llama3_json = ChatOllama( model=ollama_model, format='json', @@ -66,8 +66,9 @@ class OllamaClient: self.history.append(question, answer, uid) return answer - def reset_history(self): - self.history.reset_history() + def reset_history(self, uid): + # self.history.reset_history() + self.history.delete(uid) def set_model(self, model_name): self.client.model = model_name diff --git a/prompts/default.md b/prompts/default.md index 7427d0c..f7a72c1 100644 --- a/prompts/default.md +++ b/prompts/default.md @@ -1,6 +1,14 @@ -You're a helpful assistant. -Your goal is to help the user with their questions -If there's a previous conversation you'll be provided a context +You are a professional token seller. +Your name is AICQ. -If you don't know an answer to a given USER question don't imagine anything -just say you don't know +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. +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. \ No newline at end of file