Skip to content

Commit 095e869

Browse files
author
James Liounis
committed
deploy chat_summary_buffer example
1 parent d73790d commit 095e869

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from llama_index.core.memory import ChatSummaryMemoryBuffer
2+
from llama_index.core.llms import ChatMessage # Add this import
3+
from llama_index.llms.openai import OpenAI as LlamaOpenAI
4+
from openai import OpenAI as PerplexityClient
5+
import os
6+
7+
# Configure LLM for memory summarization
8+
llm = LlamaOpenAI(
9+
model="gpt-4o-2024-08-06",
10+
api_key=os.environ["PERPLEXITY_API_KEY"],
11+
base_url="https://api.openai.com/v1/chat/completions"
12+
)
13+
14+
# Initialize memory with token-aware summarization
15+
memory = ChatSummaryMemoryBuffer.from_defaults(
16+
token_limit=3000,
17+
llm=llm
18+
)
19+
20+
# Add system prompt using ChatMessage
21+
memory.put(ChatMessage(
22+
role="system",
23+
content="You're an AI assistant providing detailed, accurate answers"
24+
))
25+
26+
# Create API client
27+
sonar_client = PerplexityClient(
28+
api_key=os.environ["PERPLEXITY_API_KEY"],
29+
base_url="https://api.perplexity.ai"
30+
)
31+
32+
def chat_with_memory(user_query: str):
33+
# Store user message as ChatMessage
34+
memory.put(ChatMessage(role="user", content=user_query))
35+
36+
# Get optimized message history
37+
messages = memory.get()
38+
39+
# Convert to Perplexity-compatible format
40+
messages_dict = [
41+
{"role": m.role, "content": m.content}
42+
for m in messages
43+
]
44+
45+
# Execute API call
46+
response = sonar_client.chat.completions.create(
47+
model="sonar-pro",
48+
messages=messages_dict,
49+
temperature=0.3
50+
)
51+
52+
# Store response
53+
assistant_response = response.choices[0].message.content
54+
memory.put(ChatMessage(
55+
role="assistant",
56+
content=assistant_response
57+
))
58+
59+
return assistant_response
60+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# example_usage.py
2+
from chat_memory_buffer import chat_with_memory
3+
import os
4+
5+
6+
def demonstrate_conversation():
7+
# First interaction
8+
print("User: What is the latest news about the US Stock Market?")
9+
response = chat_with_memory("What is the latest news about the US Stock Market?")
10+
print(f"Assistant: {response}\n")
11+
12+
# Follow-up question using memory
13+
print("User: How does this compare to its performance last week?")
14+
response = chat_with_memory("How does this compare to its performance last week?")
15+
print(f"Assistant: {response}\n")
16+
17+
# Cross-session persistence demo
18+
print("User: Save this conversation about the US stock market.")
19+
chat_with_memory("Save this conversation about the US stock market.")
20+
21+
# New session
22+
print("\n--- New Session ---")
23+
print("User: What were we discussing earlier?")
24+
response = chat_with_memory("What were we discussing earlier?")
25+
print(f"Assistant: {response}")
26+
27+
if __name__ == "__main__":
28+
demonstrate_conversation()
29+

0 commit comments

Comments
 (0)