-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_ai.py
More file actions
50 lines (39 loc) · 1.6 KB
/
Copy pathget_ai.py
File metadata and controls
50 lines (39 loc) · 1.6 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
47
48
49
50
from langchain_cohere import ChatCohere
from langchain_core.chat_history import (
BaseChatMessageHistory,
InMemoryChatMessageHistory,
)
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.messages import HumanMessage
import os
import getpass
import logging
os.environ["COHERE_API_KEY"] = getpass.getpass() # Your cohere API
model = ChatCohere(model="command-r-plus")
store = {}
def get_session_history(session_id: str) -> BaseChatMessageHistory:
if session_id not in store:
store[session_id] = InMemoryChatMessageHistory()
return store[session_id]
with_message_history = RunnableWithMessageHistory(model, get_session_history)
config = {"configurable": {"session_id" : "user1"}}
prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"You will be given a certain text your task is to summerize that text and based on that highlight the key points in that text and create a pending task as a reminder based on the text, DO NOT IN ANY CASE GET OUT OF THE CONTEXT OF THE TEXT PROVIDED",
),
MessagesPlaceholder(variable_name="messages"),
]
)
chain = prompt | model
with_message_history = RunnableWithMessageHistory(chain, get_session_history)
def invoke_model(human_msg):
try:
response = with_message_history.invoke(
{"message": [HumanMessage(content=human_msg)] },
config=config)
return response.content
except Exception as e:
logging.error(f"Error while invoking model: {e}")