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,21 @@ 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 (str (intent_functions ))
158+
159+ max_context_len = max_comp_model_tokens - query_length - MAX_OUTPUT_TOKENS - empty_prompt_length - history_length - functions_length - len (str (dd ['function_call' ]['arguments' ])) - 1
160+
179161 print ("max_context_len" , max_context_len )
180162 search_results = completion_enc .decode (completion_enc .encode (search_results )[:max_context_len ])
181163
182-
183164 messages .append ( # adding assistant response to messages
184165 {
185166 "role" : dd ["role" ],
186- "name" : dd ["function_call" ]["name" ],
187- "content" : str (dd ['function_call' ]['arguments' ]['search_terms' ])
167+ "function_call" : {
168+ "name" : dd ["function_call" ]["name" ],
169+ "arguments" : str (dd ['function_call' ]['arguments' ])
170+ },
171+ "content" : None
188172 }
189173 )
190174 messages .append (
@@ -194,22 +178,19 @@ def chat(self, query, lc_agent, history):
194178 "content" : str (search_results ),
195179 }
196180 )
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-
181+ print ("search_results" , len (search_results ), search_results )
182+ print ('total tokens' , len (completion_enc .encode (str (messages ))))
183+ answer = openai_helpers .contact_openai (messages , completion_model = CHOSEN_COMP_MODEL )
204184
205185 else :
206186 answer = dd ['content' ]
207187
208188 return answer
209189
210190
191+
211192 def run (self , query , lc_agent = None , history = None ):
212193
213194 answer = self .chat (query , lc_agent , history )
214-
195+ print ( answer )
215196 return answer
0 commit comments