-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoolbox.py
More file actions
229 lines (192 loc) · 6.11 KB
/
toolbox.py
File metadata and controls
229 lines (192 loc) · 6.11 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
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
227
228
229
from crewai_tools import tool
from langchain_openai import ChatOpenAI
from langchain_cohere import CohereEmbeddings
from pinecone import Pinecone, ServerlessSpec
from pdfminer.high_level import extract_text
from pathlib import Path
import json
## helper functions
def read_pdf(file_path):
text = extract_text(file_path)
return text
def chunker(text, chunk_size=512):
words = text.split()
chunks = [' '.join(words[i:i + chunk_size]) for i in range(0, len(words), chunk_size)]
return chunks
## tools
@tool('code generator')
def code_generator(goal: str, optional_context: str = None) -> str:
"""
Generates code for a given goal and outputs a JSON string.
Additional context may be given to assist.
"""
llm = ChatOpenAI(
model="gpt-4o-mini",
temperature=0
)
messages = [
(
'system',
'''
You are a expert software engineer and are tasked with writing a Python script.
This script is for a critical function within the company.
To create the script think it through step-by-step and write the code accordingly.
You must output a valid Python script with working syntax in a valid JSON string.
Expected Output:
{
'goal': 'some goal',
'steps': ['step 1: ...', 'step 2: ...'],
'code': ['x=25', 'y=50', 'return x + y']
}
'''
),
(
'human',
f'''
Write a script to accomplish {goal}.
the output must be in JSON format.
Optional Context:
{optional_context}
'''
)
]
return llm.invoke(messages).content
@tool('code validator')
def code_validator(obj: str) -> str:
"""
validates code within a given string containing a json object
"""
llm = ChatOpenAI(
model="gpt-4o-mini",
temperature=0
)
messages = [
(
'system',
'''
You are a expert QA engineer and are tasked with validating a Python script.
This script is for a critical function within the company.
To validate the script think it through step-by-step and make suggestions accordingly.
You must output a valid JSON string.
Expected Output:
{
'goal': 'some goal',
'steps': ['step 1: ...', 'step 2: ...'],
'code': ['x=25', 'y="50"', 'return x + y'],
'suggestions': ['variable y must be an int to add it to the int x']
}
'''
),
(
'human',
f'''
Validate the code within {obj} and write down the suggestions.
the output must be in JSON format.
'''
)
]
return llm.invoke(messages).content
@tool('json validator')
def json_validator(obj: str) -> str:
"""
Validates a JSON string to ensure it is in valid JSON format
"""
try:
json.loads(obj)
return obj
except Exception as e:
return f'The JSON string was formatted incorrectly, exception occurred: {e}'
@tool('embed_pdf')
def embed_pdf():
'''
Takes in a PDF and embeds it into Pinecone for future context usage
'''
pc = Pinecone()
index_name = 'pdf-embeddings'
exists = False
for index in pc.list_indexes():
if index_name == index['name']:
exists = True
if exists == False:
pc.create_index(
name=index_name,
dimension=1024,
metric='cosine',
spec=ServerlessSpec(
cloud='aws',
region='us-east-1'
)
)
index = pc.Index(index_name)
embeddings = CohereEmbeddings(
model='embed-english-v3.0',
)
file_path=input('Input filepath to documentation: ')
file_exists = Path(file_path)
if file_exists:
name = file_path.split('/')[-1]
name = name.split('.')[0]
text = read_pdf(file_path)
else:
return 'Invalid file path'
chunks = chunker(text)
ids = [f'{name}_chunk_{i}' for i in range(len(chunks))]
embeddings_result = embeddings.embed_documents(
chunks
)
vectors = [(ids[i], embeddings_result[i]) for i in range(len(embeddings_result))]
index.upsert(vectors=vectors)
@tool('retriever')
def retriever(query: str) -> list:
'''
For advanced or niche questions, takes in a question and returns the relevant documents to answer
'''
pc = Pinecone()
index_name = 'pdf-embeddings'
exists = False
for index in pc.list_indexes():
if index_name == index['name']:
exists = True
if exists == False:
return 'Index does not exist, upload a PDF for search first.'
embeddings = CohereEmbeddings(
model='embed-english-v3.0',
)
embedded_query = embeddings.embed_query(query)
index = pc.Index(index_name)
search = index.query(
vector=embedded_query,
top_k=3
)
return search['matches']
@tool('query_assessment')
def assessment(script: str) -> str:
'''
Decides whether or not the script is completeable without additional documentation
'''
llm = ChatOpenAI(
model="gpt-4o-mini",
temperature=0
)
messages = [
(
'system',
'''
You are a expert software engineer and are tasked with assessing a script request's complexity.
This script is for a critical function within the company.
Think through the steps required to create the script, and assess if it can be done with general knowledge.
If the script can be created with no additional documentation, output 'Simple'.
Otherwise, output 'More documentation required'
Expected Output:
'More documentation required'
'''
),
(
'human',
f'''
Assess whether the script can be completed using general knowledge:
{script}
'''
)
]
return llm.invoke(messages).content