|
import openai |
|
import os |
|
import uuid |
|
from dotenv import load_dotenv |
|
|
|
from tenacity import ( |
|
retry, |
|
stop_after_attempt, |
|
wait_random_exponential, |
|
) |
|
|
|
|
|
|
|
load_dotenv() |
|
|
|
llm_model_name = "gpt-3.5-turbo-16k" |
|
|
|
|
|
openai.api_key = os.getenv("OPENAI_API_KEY") |
|
if os.getenv("OPENAI_API_BASE"): |
|
openai.api_base = os.getenv("OPENAI_API_BASE") |
|
stability_api_key = os.getenv("STABILITY_API_KEY") |
|
anthropic_api_key = os.getenv("ANTHROPIC_API_KEY") |
|
|
|
print("============ config info ===============\n") |
|
print("OPENAI_API_KEY:" + openai.api_key +"\n") |
|
print("OPENAI_API_BASE:" + openai.api_base +"\n") |
|
print("STABILITY_API_KEY:" + str(stability_api_key) +"\n") |
|
print("ANTHROPIC_API_KEY:" + str(anthropic_api_key) +"\n") |
|
|
|
@retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6)) |
|
def completion_with_backoff(**kwargs): |
|
return openai.ChatCompletion.create(**kwargs) |
|
|
|
|
|
|
|
def generate_uuid(): |
|
|
|
id = uuid.uuid4().hex |
|
return id |
|
|
|
|
|
|
|
def save_novel_chapter(novel_id, chapter_index, file_name, file_content): |
|
|
|
chapter_folder = os.path.join(os.getcwd(), f"story/{novel_id}/chapter_{chapter_index + 1}") |
|
if not os.path.exists(chapter_folder): |
|
os.makedirs(chapter_folder) |
|
|
|
|
|
file_path = os.path.join(chapter_folder, f"{file_name}.txt") |
|
with open(file_path, "w") as file: |
|
file.write(file_content) |