HMC-CIS/HMC-CIS-chatbot-UI-testing
0
1import os2import openai3"""4A module to manage responses from the OpenAI Response API for an IT Helpdesk assistant 5at Harvey Mudd College. This module initializes the OpenAI client and provides a method 6to create responses using RAG (Retrieval-Augmented Generation) to user queries. It uses 7a vector store for retrieval of knowledge base documents and generates responses using 8the specified OpenAI model. The module also loads a developer message from a text file 9to prompt engineer responses from the AI model.10"""11 12# Load the OpenAI API key from the environment variable13# If the API key is not set, raise an error.14if "OPENAI_API_KEY" not in os.environ:15 raise ValueError("OPENAI_API_KEY environment variable is not set.")16api_key=os.getenv("OPENAI_API_KEY")17 18class ResponseManager:19 """20 A class to manage responses from the OpenAI API for an IT Helpdesk assistant.21 This class initializes the OpenAI client and provides a method to create responses22 to user queries using the specified OpenAI model.23 """24 def __init__(self, vector_store_id):25 """26 Initialize the ResponseManager with a vector store ID.27 :param vector_store_id: The ID of the vector store to use for file search.28 """29 # Initialize the OpenAI client30 # Note: The OpenAI client is initialized with the API key set in the environment variable31 # This is a placeholder for the actual OpenAI client initialization32 # In a real-world scenario, you would use the appropriate OpenAI client library33 # For example, if using the OpenAI Python library, you would do: 34 self.client = openai.OpenAI(api_key=api_key)35 self.vector_store_id = vector_store_id36 self.previous_response_id = None37 38 # Load the meta prompt from the text file39 # This message is used to provide context for the AI model40 meta_prompt_file = 'config/meta_prompt.txt'41 if not os.path.exists(meta_prompt_file):42 raise FileNotFoundError(f"Meta prompt file '{meta_prompt_file}' not found.")43 with open(meta_prompt_file, 'r') as file:44 self.meta_prompt = file.read().strip()45 46 def create_response(self, query, model: str= "gpt-4o-mini",47 temperature=0, max_output_tokens=800,48 max_num_results=7):49 """50 Create a response to a user query using the OpenAI API.51 :param query: The user query to respond to.52 :param model: The OpenAI model to use (default is "gpt-4o-mini").53 :param temperature: The temperature for the response (default is 0).54 :param max_output_tokens: The maximum number of output tokens (default is 800).55 :param max_num_results: The maximum number of search results to return (default is 7).56 :param verbose: Whether to print the response (default is False).57 :return: The response text from the OpenAI API.58 """59 if self.previous_response_id is None:60 input=[{"role": "developer", "content": self.meta_prompt}, 61 {"role": "user", "content": query}]62 else:63 input=[{"role": "user", "content": query}]64 65 response = self.client.responses.create(66 model=model,67 previous_response_id=self.previous_response_id,68 input=input,69 tools=[{70 "type": "file_search",71 "vector_store_ids": [self.vector_store_id], # ["<vector_store_id>"]72 "max_num_results": max_num_results}73 ],74 temperature=temperature,75 max_output_tokens = max_output_tokens,76 # include=["output[*].file_search_call.search_results"]77 )78 self.previous_response_id = response.id79 80 return response.output_text