Spaces:
Sleeping
Sleeping
File size: 2,632 Bytes
b60b6c5 881fcd3 b60b6c5 881fcd3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
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
|