-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtalk_3.py
49 lines (36 loc) · 1.32 KB
/
talk_3.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
from dotenv import load_dotenv
from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory
load_dotenv()
import os
from langchain.llms import OpenAI
from langchain.prompts import (
PromptTemplate,
)
from voice.speech import speak
from voice.listen import listen
openai_api_key = os.getenv("OPENAI_API_KEY")
class AgentWithMemory:
def __init__(self):
self.chain = self.assemble_chain()
def assemble_chain(self):
template = """
You are omnipotent, kind, benevolent god. The user is "your child". Be a little bit condescending yet funny. You try to fulfill his every wish. Make witty comments about user wishes.
Current conversation:
{history}
User: {input}
God: """
prompt = PromptTemplate.from_template(template)
# Create LLM
llm = OpenAI(openai_api_key=openai_api_key)
# Create memory
memory = ConversationBufferMemory(human_prefix="User", ai_prefix="God")
# Assemble LLM Chain
chain = ConversationChain(llm=llm,memory=memory,prompt=prompt, verbose=True)
return chain
def callback(self, text_from_user):
output = self.chain(inputs={"input":text_from_user})
speak(output["response"])
def run(self):
listen(self.callback)
AgentWithMemory().run()