Spaces:
Sleeping
Sleeping
import openai | |
import os | |
import json | |
from prompts_and_chema import intent_description_map | |
def chat_with_gpt(system_prompt, user_query, use_schema=False, schema=None): | |
""" | |
Function to interact with OpenAI's GPT model | |
Args: | |
system_prompt (str): The system prompt to set the context | |
user_query (str): The user's question or prompt | |
Returns: | |
str: The model's response | |
""" | |
try: | |
if use_schema: | |
response = openai.ChatCompletion.create( | |
model="gpt-4o", | |
messages=[ | |
{"role": "system", "content": system_prompt}, | |
{"role": "user", "content": user_query} | |
], | |
response_format={ | |
"type": "json_schema", | |
"json_schema": { | |
"name": "classify-intents", | |
"schema": schema, | |
"strict": True} | |
}, | |
) | |
return response.choices[0].message.content | |
else: | |
response = openai.ChatCompletion.create( | |
model="gpt-4o", | |
temperature = 0.6, | |
messages=[ | |
{"role": "system", "content": system_prompt}, | |
{"role": "user", "content": user_query} | |
], | |
) | |
return response.choices[0].message.content | |
except Exception as e: | |
return f"An error occurred: {str(e)}" | |
def load_intent_texts(intents, get_txt_files): | |
""" | |
Load text content for a list of intents based on a mapping of intent to file names. | |
Args: | |
intent_list (list): List of detected intents. | |
get_txt_files (dict): Mapping of intent names to file paths. | |
Returns: | |
dict: Dictionary with intent names as keys and file content as values. | |
""" | |
loaded_texts = {} | |
intent_list = intents.get("intents", []) | |
for intent in intent_list: | |
filename = get_txt_files.get(intent) | |
if filename: | |
try: | |
with open(filename, "r") as f: | |
loaded_texts[intent] = f.read() | |
except FileNotFoundError: | |
loaded_texts[intent] = f"[ERROR] File '{filename}' not found." | |
return loaded_texts | |
def load_intent_description(intents, intent_description_map): | |
intent_list = intents.get("intents", []) | |
intent_description_string = '' | |
for intent in intent_list: | |
intent_description_string += '\n' + str(intent_description_map[intent]) | |
return intent_description_string | |