added bot prediction for assessments
This commit is contained in:
+120
-1
@@ -7,6 +7,7 @@ from src.prompts.sops import *
|
||||
from src.prompts.chatbot import *
|
||||
from src.models.sop_response_schemas import *
|
||||
from src.models.bot_response_schema import *
|
||||
from scripts.assessment_data import generate_summary_stats_v2
|
||||
from dotenv import load_dotenv
|
||||
load_dotenv()
|
||||
|
||||
@@ -52,7 +53,7 @@ class Chatbot:
|
||||
}
|
||||
],
|
||||
response_format=ValidateWorker,
|
||||
max_tokens=4096,
|
||||
max_tokens=1024,
|
||||
temperature=0.1
|
||||
)
|
||||
|
||||
@@ -64,3 +65,121 @@ class Chatbot:
|
||||
except Exception as e:
|
||||
print(f"An error occurred: {e}")
|
||||
return None
|
||||
|
||||
|
||||
|
||||
|
||||
def predict_based_on_past_assessment(self, query, company_info, companyid) -> Result:
|
||||
"""
|
||||
This method generates predictions based on past assessment data of a company. It queries the backend for the
|
||||
company's assessment data, generates a prompt, and then uses the GPT-4 model to return predictions based on the query.
|
||||
|
||||
:param query: The question or query asked by the user.
|
||||
:param company_info: General information about the company (name, size, departments, etc.).
|
||||
:param companyid: Unique identifier of the company to fetch its specific data.
|
||||
:return: Result containing the prediction result or None if an error occurs.
|
||||
"""
|
||||
try:
|
||||
# Define the path to the company's assessment data (stored as a CSV)
|
||||
data_path = os.path.join('data', 'raw', 'erp_company_assessment', f'{companyid}_raw_data.csv')
|
||||
|
||||
# Generate summary statistics from the company's assessment data
|
||||
summary_stats = generate_summary_stats_v2(file_path=data_path)
|
||||
|
||||
|
||||
# Generate the prompt using the company info and the summary statistics
|
||||
prompt = predict_based_past_assessment_prompt(
|
||||
query=query,
|
||||
company_info=company_info,
|
||||
summary_stats=summary_stats
|
||||
)
|
||||
|
||||
# Interact with GPT-4 model to get a response
|
||||
response = self.client.beta.chat.completions.parse(
|
||||
model=self.model,
|
||||
messages=[
|
||||
{
|
||||
"role": "system",
|
||||
"content": f"{prompt}"
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": f"{query}",
|
||||
}
|
||||
],
|
||||
response_format=Result,
|
||||
max_tokens=1024,
|
||||
temperature=0.1
|
||||
)
|
||||
|
||||
# Extract and return the response from the GPT-4 model
|
||||
extracted_text = json.loads(response.choices[0].message.content)
|
||||
|
||||
return extracted_text
|
||||
|
||||
except Exception as e:
|
||||
print(f"An error occurred: {e}")
|
||||
return None
|
||||
|
||||
|
||||
def predict_next_n_assessment(self, company_info, companyid, N) -> AssessmentPredictionsResponse:
|
||||
"""
|
||||
This method generates predictions based on past assessment data of a company. It queries the backend for the
|
||||
company's assessment data, generates a prompt, and then uses the GPT-4 model to return predictions based on the query.
|
||||
|
||||
:param query: The question or query asked by the user.
|
||||
:param company_info: General information about the company (name, size, departments, etc.).
|
||||
:param companyid: Unique identifier of the company to fetch its specific data.
|
||||
:param N: Number of assessments to predict.
|
||||
:return: Result containing the prediction result or None if an error occurs.
|
||||
"""
|
||||
try:
|
||||
# Define the path to the company's assessment data (stored as a CSV)
|
||||
data_path = os.path.join('data', 'raw', 'erp_company_assessment', f'{companyid}_raw_data.csv')
|
||||
|
||||
# Generate summary statistics from the company's assessment data
|
||||
summary_stats = generate_summary_stats_v2(file_path=data_path)
|
||||
|
||||
# Generate the prompt using the company info and the summary statistics
|
||||
prompt = predict_next_n_assessments_prompt()
|
||||
|
||||
|
||||
# Interact with GPT-4 model to get a response
|
||||
response = self.client.beta.chat.completions.parse(
|
||||
model=self.model,
|
||||
messages=[
|
||||
{
|
||||
"role": "system",
|
||||
"content": f"{prompt}"
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": f"company info: {company_info}--> N-value is {N} ",
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": f"Summary stats: {summary_stats}",
|
||||
}
|
||||
],
|
||||
response_format=AssessmentPredictionsResponse,
|
||||
max_tokens=1024,
|
||||
temperature=0.1
|
||||
)
|
||||
|
||||
# Extract the response from the GPT-4 model
|
||||
extracted_text = json.loads(response.choices[0].message.content)
|
||||
|
||||
# Initialize dictionary to store assessments with dynamic names
|
||||
predictions = {}
|
||||
|
||||
# Loop through the predicted assessments and rename them dynamically
|
||||
for i in range(N):
|
||||
assessment_key = f"assessment_{i + 1}"
|
||||
predictions[assessment_key] = extracted_text["predictions"][i]['AssessmentN']
|
||||
|
||||
# Return the dynamically named assessments
|
||||
return predictions
|
||||
|
||||
except Exception as e:
|
||||
print(f"An error occurred: {e}")
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user