-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession_chain.py
More file actions
46 lines (39 loc) · 1.41 KB
/
session_chain.py
File metadata and controls
46 lines (39 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from langchain_groq import ChatGroq
from langchain.memory import ConversationBufferWindowMemory
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain.chains import ConversationChain
from dotenv import load_dotenv
import os
load_dotenv()
class SessionChain:
def __init__(self, session_id):
self.session_id = session_id
self.chain = self.create_chain()
def create_chain(self):
llm = ChatGroq(
groq_api_key=os.getenv("GROQ_API_KEY"),
model_name="llama-3.3-70b-versatile",
temperature=0.7,
max_tokens=1000,
streaming=False
)
memory = ConversationBufferWindowMemory(
k=10,
memory_key="chat_history",
return_messages=True
)
# Create prompt template
prompt = ChatPromptTemplate.from_messages([
("system", """You are a helpful AI assistant. Provide clear, concise, and engaging responses.
Be conversational but informative. If you're unsure about something, acknowledge it."""),
MessagesPlaceholder(variable_name="chat_history"),
("human", "{input}")
])
# Create conversation chain
chain = ConversationChain(
llm=llm,
memory=memory,
prompt=prompt,
verbose=True
)
return chain