44
44
45
45
46
46
47
-
48
- # intent_functions= [
49
- # {
50
- # "name": "search_knowledge_base",
51
- # "type": "function",
52
- # "description": "Search through knowledge base to find relevant documents that might help in answering the user query.",
53
- # "parameters": {
54
- # "type": "object",
55
- # "properties": {
56
- # "search_terms": {
57
- # "type": "string",
58
- # "description": "Search terms that would be used in the search engine"
59
- # }
60
- # },
61
- # "required": ["search_terms"]
62
- # }
63
- # }
64
- # ]
65
-
66
47
intent_functions = [
67
48
{
68
49
"name" : "extract_search_terms" ,
@@ -147,20 +128,19 @@ def chat(self, query, lc_agent, history):
147
128
completion_enc = openai_helpers .get_encoder (CHOSEN_COMP_MODEL )
148
129
149
130
messages .append ({"role" : "user" , "content" :intent_body .format (history = history , query = query )})
150
- # messages.append({"role": "user", "content": query} )
131
+ print ( "messages" , messages )
151
132
152
133
response = openai_helpers .contact_openai (messages , completion_model = CHOSEN_COMP_MODEL , functions = intent_functions )
153
134
154
135
dd = self .get_dict (response )
136
+
155
137
156
138
if 'function_call' in dd :
157
139
search_terms = dd ['function_call' ]['arguments' ]['search_terms' ]
158
140
search_results = []
159
141
160
142
print ("search_terms" , search_terms )
161
143
162
- # search_results.append(lc_agent.agent_cog_search(search_terms))
163
-
164
144
for s in search_terms :
165
145
search_results .append (lc_agent .agent_cog_search (s ['term' ] + ' ' + s .get ('additional_context' , '' )))
166
146
@@ -174,17 +154,23 @@ def chat(self, query, lc_agent, history):
174
154
query_length = len (completion_enc .encode (query ))
175
155
history_length = len (completion_enc .encode (history ))
176
156
177
- max_context_len = max_comp_model_tokens - query_length - MAX_OUTPUT_TOKENS - empty_prompt_length - history_length - 1
178
-
157
+ functions_length = len (completion_enc .encode (str (intent_functions )))
158
+ func_args_length = len (completion_enc .encode (str (dd ['function_call' ]['arguments' ])))
159
+
160
+ max_context_len = max_comp_model_tokens - query_length - MAX_OUTPUT_TOKENS - empty_prompt_length - history_length - functions_length - func_args_length - 1
161
+ print (max_context_len , max_comp_model_tokens , query_length , MAX_OUTPUT_TOKENS , empty_prompt_length , history_length , functions_length , func_args_length )
162
+
179
163
print ("max_context_len" , max_context_len )
180
164
search_results = completion_enc .decode (completion_enc .encode (search_results )[:max_context_len ])
181
165
182
-
183
166
messages .append ( # adding assistant response to messages
184
167
{
185
168
"role" : dd ["role" ],
186
- "name" : dd ["function_call" ]["name" ],
187
- "content" : str (dd ['function_call' ]['arguments' ]['search_terms' ])
169
+ "function_call" : {
170
+ "name" : dd ["function_call" ]["name" ],
171
+ "arguments" : str (dd ['function_call' ]['arguments' ])
172
+ },
173
+ "content" : None
188
174
}
189
175
)
190
176
messages .append (
@@ -194,22 +180,19 @@ def chat(self, query, lc_agent, history):
194
180
"content" : str (search_results ),
195
181
}
196
182
)
197
-
198
- messages .append ({"role" : "user" , "content" :body .format (history = history , context = search_results , query = query )})
199
-
200
- print ("search_results" , len (completion_enc .encode (search_results )), search_results )
201
- answer = openai_helpers .contact_openai (messages , completion_model = CHOSEN_COMP_MODEL , functions = intent_functions )
202
- answer = answer ['choices' ][0 ]['message' ]['content' ]
203
-
183
+ print ("search_results" , len (search_results ), search_results )
184
+ print ('total tokens' , len (completion_enc .encode (str (messages ))))
185
+ answer = openai_helpers .contact_openai (messages , completion_model = CHOSEN_COMP_MODEL )
204
186
205
187
else :
206
188
answer = dd ['content' ]
207
189
208
190
return answer
209
191
210
192
193
+
211
194
def run (self , query , lc_agent = None , history = None ):
212
195
213
196
answer = self .chat (query , lc_agent , history )
214
-
197
+ print ( answer )
215
198
return answer
0 commit comments