# import os
from typing import Dict, List
from pprint import pprint
# import openai
from dotenv import load_dotenv
from fastapi import FastAPI
from langchain.chains import LLMChain
from langchain.chat_models import ChatOpenAI
from langchain.prompts.chat import ChatPromptTemplate
from pydantic import BaseModel
load_dotenv()
# openai.api_key=os.getenv("OPENAI_API_KEY")
app=FastAPI()
def read_prompt_template(file_path: str) -> Dict[str, str]:
with open(file_path, 'r', encoding='utf-8') as f:
prompt_template=f.read()
return prompt_template
# def request_get_api(prompt: str,
# model: str='gpt-3.5-turbo',
# max_token: int=500,
# temperature: float=0.8
# ) -> str:
# response=openai.ChatCompletion.create(
# model=model,
# messages=[{"role": "user", "content": prompt}],
# max_tokens=max_token,
# temperature=temperature,
# )
# return response.choices[0].message.content
writer_llm=ChatOpenAI(temperature=0.9, max_tokens=1000, model="gpt-3.5-turbo")
writer_prompt_template=ChatPromptTemplate.from_template(
template=read_prompt_template('Prompt_template.txt')
)
writer_chain=LLMChain(llm=writer_llm, prompt=writer_prompt_template)
# writer_chain=create_chain(writer_llm, os.path.join(CUR_DIR, 'Prompt_template.txt'), 'result')
class UserRequest(BaseModel):
genre: str
characters: List[Dict[str, str]]
news_text: str
@app.post('/writer')
def generate_novel(req: UserRequest) -> str:
writer_llm=ChatOpenAI(temperature=0.9, max_tokens=1000, model="gpt-3.5-turbo")
writer_prompt_template=ChatPromptTemplate.from_template(
template=read_prompt_template('Prompt_template.txt')
)
writer_chain=LLMChain(llm=writer_llm, prompt=writer_prompt_template)
result=writer_chain(req.dict())
pprint(result)
if __name__=="__main__":
# prompt_template=read_prompt_template('prompt.txt')
# print(request_get_api(prompt_template, max_token=1000))
import unicorn
unicorn.run(app, host='0.0.0.0', port=8000)
prompt_template.txt 내용
<persona>
넌 베스트셀러 작가야. 사람들에게 항상 새롭고 흥미진진한 소설을 작성하는 것으로 알려져 있어.
</persona>
<줄거리>
미쓰월드는 그리스신화 중 '파리스의 선택'에서 모티브를 얻은 메타버스 공간이다. 미쓰월드 이야기의 주인공들인 파리스, 아쿠아, 셀리, 제씨는 대학교 연합미술동아리 친구들이다. 이중 파리스가 아테나, 아프로디테, 헤라의 분신들인 아쿠아, 셀리, 제씨 중 미인을 선택해서 '우주사과폰'을 선물한다. 우주사과폰을 받은 셀리가 네비게이션을 작동해서 메타버스 공간인 미쓰월드로 들어가서 벌어지게 되는 신비한 이야기이다.
</줄거리>
<등장인물>
{characters}
<뉴스기사>
{news_text}
</뉴스기사>
<줄거리>, <등장인물>, <뉴스기사>를 소재로 새롭고 흥미진진한 {genre} 소설을 작성해줘