4444
4545
4646
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-
6647intent_functions = [
6748 {
6849 "name" : "extract_search_terms" ,
@@ -147,20 +128,19 @@ def chat(self, query, lc_agent, history):
147128 completion_enc = openai_helpers .get_encoder (CHOSEN_COMP_MODEL )
148129
149130 messages .append ({"role" : "user" , "content" :intent_body .format (history = history , query = query )})
150- # messages.append({"role": "user", "content": query} )
131+ print ( "messages" , messages )
151132
152133 response = openai_helpers .contact_openai (messages , completion_model = CHOSEN_COMP_MODEL , functions = intent_functions )
153134
154135 dd = self .get_dict (response )
136+
155137
156138 if 'function_call' in dd :
157139 search_terms = dd ['function_call' ]['arguments' ]['search_terms' ]
158140 search_results = []
159141
160142 print ("search_terms" , search_terms )
161143
162- # search_results.append(lc_agent.agent_cog_search(search_terms))
163-
164144 for s in search_terms :
165145 search_results .append (lc_agent .agent_cog_search (s ['term' ] + ' ' + s .get ('additional_context' , '' )))
166146
@@ -174,17 +154,23 @@ def chat(self, query, lc_agent, history):
174154 query_length = len (completion_enc .encode (query ))
175155 history_length = len (completion_enc .encode (history ))
176156
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+
179163 print ("max_context_len" , max_context_len )
180164 search_results = completion_enc .decode (completion_enc .encode (search_results )[:max_context_len ])
181165
182-
183166 messages .append ( # adding assistant response to messages
184167 {
185168 "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
188174 }
189175 )
190176 messages .append (
@@ -194,22 +180,19 @@ def chat(self, query, lc_agent, history):
194180 "content" : str (search_results ),
195181 }
196182 )
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 )
204186
205187 else :
206188 answer = dd ['content' ]
207189
208190 return answer
209191
210192
193+
211194 def run (self , query , lc_agent = None , history = None ):
212195
213196 answer = self .chat (query , lc_agent , history )
214-
197+ print ( answer )
215198 return answer
0 commit comments