File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -64,6 +64,7 @@ import anthropic
6464
6565chroma = Client()
6666collection = chroma.get_or_create_collection(" chat_history" )
67+ client = anthropic.Anthropic()
6768
6869def chat_with_memory (user_message , conversation_id ):
6970 # 检索相关历史
@@ -77,21 +78,22 @@ def chat_with_memory(user_message, conversation_id):
7778 context = f " 相关历史记录: \n { relevant_history[' documents' ]} "
7879
7980 # 调用 Claude
80- response = anthropic .messages.create(
81+ response = client .messages.create(
8182 model = " claude-sonnet-4-6" ,
83+ system = context,
8284 messages = [
83- {" role" : " system" , " content" : context},
8485 {" role" : " user" , " content" : user_message}
8586 ]
8687 )
8788
8889 # 存储新对话
90+ reply = response.content[0 ].text
8991 collection.add(
90- documents = [f " User: { user_message} \n Assistant: { response.content } " ],
92+ documents = [f " User: { user_message} \n Assistant: { reply } " ],
9193 metadatas = [{" conversation_id" : conversation_id}]
9294 )
9395
94- return response.content
96+ return reply
9597```
9698
9799### 10.3.5 原生上下文压缩
You can’t perform that action at this time.
0 commit comments