@@ -459,75 +459,6 @@ def __init__(self, title: str, logger: logging.Logger, api_url: str, model: str,
459459 headers = headers ,
460460 params = params
461461 )
462-
463- def get_chatbot_answer (self , prompt : str ) -> dict :
464- """
465- Sends a query to the chatbot API and retrieves the resulting response. This method constructs
466- the request body for the chatbot, sends the request to the API, and parses the response.
467-
468- Parameters
469- ----------
470- prompt : str
471- The prompt or message to send to the chatbot for a response.
472-
473- Returns
474- -------
475- dict
476- The parsed response from the chatbot API, containing the chatbot's answer.
477- """
478- request_body = self ._generate_query (prompt )
479- response = self .api_call .make_api_request (method = "POST" , request_body = request_body ) # Delegate API call
480- if response :
481- self .logger .info ("Request successful" )
482- else :
483- self .logger .warning ("No response retrieved." )
484- parsed_response = self ._parse_api_response (response )
485- return parsed_response
486-
487-
488- def _generate_query (self , messages : str ) -> dict :
489- """
490- Constructs the request body for the chatbot query.
491-
492- Parameters
493- ----------
494- messages : str
495- The messages to send to the chatbot.
496-
497- Returns
498- -------
499- dict
500- The constructed request body for the chatbot query.
501- """
502- self .logger .info (f"Generating request body for message: { messages } " )
503- return {
504- "model" : self .model ,
505- "messages" : messages ,
506- "stream" : True
507- }
508-
509- def _parse_api_response (self , response : dict ) -> dict :
510- """
511- Processes and extracts relevant data from the API response.
512-
513- Parameters
514- ----------
515- response : dict
516- The response from the API.
517-
518- Returns
519- -------
520- dict
521- The processed response, including the role and content of the assistant's reply.
522- """
523- output = ""
524- for line in response .iter_lines ():
525- body = json .loads (line )
526- if "error" in body :
527- raise Exception (body ["error" ])
528- if body .get ("done" , False ):
529- return {"role" : "assistant" , "content" : output }
530- output += body .get ("message" , {}).get ("content" , "" )
531462
532463@dataclass
533464class Subsection :
0 commit comments