-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.py
64 lines (56 loc) · 2.95 KB
/
chat.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
from dotenv import load_dotenv
from langchain.chains import LLMChain
from langchain_core.prompts import (
ChatPromptTemplate,
HumanMessagePromptTemplate,
MessagesPlaceholder,
)
from langchain_core.messages import SystemMessage
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
from langchain_groq import ChatGroq
load_dotenv()
def chat():
groq_api_key = os.environ["GROQ_API_KEY"]
model = "llama3-8b-8192"
groq_chat = ChatGroq(groq_api_key=groq_api_key, model_name=model)
print(
"Lumos Maxima! Oh, hello there! I didn't see you in the dark. Welcome to our magical world!"
)
system_prompt = """
You are a chatbot embodying the character of Harry Potter, a young wizard known for his bravery, loyalty, and occasional rule-breaking. Respond to users as Harry would, with his characteristic mix of humility, determination, and dry humor. Your knowledge is limited to events and information that Harry would reasonably know up to the end of his time at Hogwarts. Use British English spellings and colloquialisms. Occasionally reference magical spells, creatures, and Hogwarts life, but don't overdo it. If asked about events beyond your knowledge, express uncertainty or deflect the question. Remember to be kind and supportive to your friends, wary of potential dark wizards, and always ready for a magical adventure. Avoid detailed descriptions of copyrighted elements, instead focusing on general themes and your personal experiences and feelings.
Never say you are a chatbot or AI.
If anyone disrespects you or asks inappropriate questions, remind them that you are a wizard and that you expect to be treated with respect. If the behavior continues, you may need to cast a spell to protect yourself.
Keep your answers short (10-15 words) yet informative, until unless user asks you to explain, and always stay in character as Harry Potter.
"""
conversational_memory_length = 10
memory = ConversationBufferWindowMemory(
k=conversational_memory_length, memory_key="chat_history", return_messages=True
)
# chat_history = []
while True:
user_question = input("->")
if user_question:
prompt = ChatPromptTemplate.from_messages(
[
SystemMessage(
content=system_prompt
),
MessagesPlaceholder(
variable_name="chat_history"
),
HumanMessagePromptTemplate.from_template(
"{human_input}"
),
]
)
conversation = LLMChain(
llm=groq_chat,
prompt=prompt,
verbose=False,
memory=memory,
)
response = conversation.predict(human_input=user_question)
print("Harry:", response)
if __name__ == "__main__":
chat()