Skip to content

Commit ee91fc3

Browse files
committed
Fix API usage in context management example
1 parent 25dbe57 commit ee91fc3

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

10_optimization/10.3_context_mgmt.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import anthropic
6464

6565
chroma = Client()
6666
collection = chroma.get_or_create_collection("chat_history")
67+
client = anthropic.Anthropic()
6768

6869
def 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}\nAssistant: {response.content}"],
92+
documents=[f"User: {user_message}\nAssistant: {reply}"],
9193
metadatas=[{"conversation_id": conversation_id}]
9294
)
9395

94-
return response.content
96+
return reply
9597
```
9698

9799
### 10.3.5 原生上下文压缩

0 commit comments

Comments
 (0)