Spaces:
Running
Running
File size: 1,350 Bytes
6c8a2d5 |
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 |
import json
import os
from openai import OpenAI
from backend.config import config, CURRICULUM_INSTRUCTION
from backend.utils import write_json_file, ensure_directory
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
output_dir = os.path.join(PROJECT_ROOT, "output")
ensure_directory(output_dir)
client = OpenAI(
api_key=config.get('api_key'),
base_url=config.get('base_url'),
)
def get_completion(prompt: str):
response = client.chat.completions.create(
model=config.get('model'),
messages=[
{"role": "system", "content": CURRICULUM_INSTRUCTION},
{"role": "user", "content": prompt},
],
response_format={"type": "json_object"},
)
try:
data = json.loads(response.choices[0].message.content)
filename = os.path.join(output_dir, "curriculum.json")
if write_json_file(filename, data):
print(f"JSON data saved to {filename}")
# Reset the config when new curriculum is generated
config.set("current_week", 0)
config.save()
return data # Return the data instead of True
print("Failed to save data")
return None
except json.JSONDecodeError as e:
print(f"Error decoding JSON: {e}")
return None
|