-
Notifications
You must be signed in to change notification settings - Fork 113
/
agent.py
226 lines (186 loc) · 8.23 KB
/
agent.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import openai
import os
import pinecone
import yaml
from dotenv import load_dotenv
import nltk
from langchain.text_splitter import NLTKTextSplitter
# Download NLTK for Reading
nltk.download('punkt')
# Initialize Text Splitter
text_splitter = NLTKTextSplitter(chunk_size=2500)
# Load default environment variables (.env)
load_dotenv()
OPENAI_MODEL = os.getenv("OPENAI_MODEL") or "gpt-4"
def generate(prompt):
completion = openai.ChatCompletion.create(
model=OPENAI_MODEL,
messages=[
{"role": "system", "content": "You are an intelligent agent with thoughts and memories. You have a memory which stores your past thoughts and actions and also how other users have interacted with you."},
{"role": "system", "content": "Keep your thoughts relatively simple and concise"},
{"role": "user", "content": prompt},
]
)
return completion.choices[0].message["content"]
PINECONE_API_KEY = os.getenv("PINECONE_API_KEY")
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
PINECONE_API_ENV = os.getenv("PINECONE_API_ENV")
#PINECONE_API_ENV = "asia-southeast1-gcp"
# Prompt Initialization
with open('prompts.yaml', 'r') as f:
data = yaml.load(f, Loader=yaml.FullLoader)
# Counter Initialization
with open('memory_count.yaml', 'r') as f:
counter = yaml.load(f, Loader=yaml.FullLoader)
# internalThoughtPrompt = data['internal_thought']
# externalThoughtPrompt = data['external_thought']
# internalMemoryPrompt = data['internal_thought_memory']
# externalMemoryPrompt = data['external_thought_memory']
# Thought types, used in Pinecone Namespace
THOUGHTS = "Thoughts"
QUERIES = "Queries"
INFORMATION = "Information"
ACTIONS = "Actions"
# Top matches length
k_n = 3
# initialize pinecone
pinecone.init(api_key=PINECONE_API_KEY, environment=PINECONE_API_ENV)
# initialize openAI
openai.api_key = OPENAI_API_KEY # you can just copy and paste your key here if you want
def get_ada_embedding(text):
text = text.replace("\n", " ")
return openai.Embedding.create(input=[text], model="text-embedding-ada-002")[
"data"
][0]["embedding"]
def read_txtFile(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
text = file.read()
return text
class Agent():
def __init__(self, table_name=None) -> None:
self.table_name = table_name
self.memory = None
self.thought_id_count = int(counter['count'])
self.last_message = ""
# Keep Remebering!
# def __del__(self) -> None:
# with open('memory_count.yaml', 'w') as f:
# yaml.dump({'count': str(self.thought_id_count)}, f)
def createIndex(self, table_name=None):
# Create Pinecone index
if(table_name):
self.table_name = table_name
if(self.table_name == None):
return
dimension = 1536
metric = "cosine"
pod_type = "p1"
if self.table_name not in pinecone.list_indexes():
pinecone.create_index(
self.table_name, dimension=dimension, metric=metric, pod_type=pod_type
)
# Give memory
self.memory = pinecone.Index(self.table_name)
# Adds new Memory to agent, types are: THOUGHTS, ACTIONS, QUERIES, INFORMATION
def updateMemory(self, new_thought, thought_type):
with open('memory_count.yaml', 'w') as f:
yaml.dump({'count': str(self.thought_id_count)}, f)
if thought_type==INFORMATION:
new_thought = "This is information fed to you by the user:\n" + new_thought
elif thought_type==QUERIES:
new_thought = "The user has said to you before:\n" + new_thought
elif thought_type==THOUGHTS:
# Not needed since already in prompts.yaml
# new_thought = "You have previously thought:\n" + new_thought
pass
elif thought_type==ACTIONS:
# Not needed since already in prompts.yaml as external thought memory
pass
vector = get_ada_embedding(new_thought)
upsert_response = self.memory.upsert(
vectors=[
{
'id':f"thought-{self.thought_id_count}",
'values':vector,
'metadata':
{"thought_string": new_thought
}
}],
namespace=thought_type,
)
self.thought_id_count += 1
# Agent thinks about given query based on top k related memories. Internal thought is passed to external thought
def internalThought(self, query) -> str:
query_embedding = get_ada_embedding(query)
query_results = self.memory.query(query_embedding, top_k=2, include_metadata=True, namespace=QUERIES)
thought_results = self.memory.query(query_embedding, top_k=2, include_metadata=True, namespace=THOUGHTS)
results = query_results.matches + thought_results.matches
sorted_results = sorted(results, key=lambda x: x.score, reverse=True)
top_matches = "\n\n".join([(str(item.metadata["thought_string"])) for item in sorted_results])
#print(top_matches)
internalThoughtPrompt = data['internal_thought']
internalThoughtPrompt = internalThoughtPrompt.replace("{query}", query).replace("{top_matches}", top_matches).replace("{last_message}", self.last_message)
print("------------INTERNAL THOUGHT PROMPT------------")
print(internalThoughtPrompt)
internal_thought = generate(internalThoughtPrompt) # OPENAI CALL: top_matches and query text is used here
# Debugging purposes
#print(internal_thought)
internalMemoryPrompt = data['internal_thought_memory']
internalMemoryPrompt = internalMemoryPrompt.replace("{query}", query).replace("{internal_thought}", internal_thought).replace("{last_message}", self.last_message)
self.updateMemory(internalMemoryPrompt, THOUGHTS)
return internal_thought, top_matches
def action(self, query) -> str:
internal_thought, top_matches = self.internalThought(query)
externalThoughtPrompt = data['external_thought']
externalThoughtPrompt = externalThoughtPrompt.replace("{query}", query).replace("{top_matches}", top_matches).replace("{internal_thought}", internal_thought).replace("{last_message}", self.last_message)
print("------------EXTERNAL THOUGHT PROMPT------------")
print(externalThoughtPrompt)
external_thought = generate(externalThoughtPrompt) # OPENAI CALL: top_matches and query text is used here
externalMemoryPrompt = data['external_thought_memory']
externalMemoryPrompt = externalMemoryPrompt.replace("{query}", query).replace("{external_thought}", external_thought)
self.updateMemory(externalMemoryPrompt, THOUGHTS)
request_memory = data["request_memory"]
self.updateMemory(request_memory.replace("{query}", query), QUERIES)
self.last_message = query
return external_thought
# Make agent think some information
def think(self, text) -> str:
self.updateMemory(text, THOUGHTS)
# Make agent read some information
def read(self, text) -> str:
texts = text_splitter.split_text(text)
vectors = []
for t in texts:
t = "This is information fed to you by the user:\n" + t
vector = get_ada_embedding(t)
vectors.append({
'id':f"thought-{self.thought_id_count}",
'values':vector,
'metadata':
{"thought_string": t,
}
})
self.thought_id_count += 1
upsert_response = self.memory.upsert(
vectors,
namespace=INFORMATION,
)
# Make agent read a document
def readDoc(self, text) -> str:
texts = text_splitter.split_text(read_txtFile(text))
vectors = []
for t in texts:
t = "This is a document fed to you by the user:\n" + t
vector = get_ada_embedding(t)
vectors.append({
'id':f"thought-{self.thought_id_count}",
'values':vector,
'metadata':
{"thought_string": t,
}
})
self.thought_id_count += 1
upsert_response = self.memory.upsert(
vectors,
namespace=INFORMATION,
)