Spaces:
Sleeping
Sleeping
from dotenv import load_dotenv | |
_ = load_dotenv() | |
import os | |
import openai | |
from langchain_core.messages import AIMessage | |
from langchain_core.prompts import PromptTemplate | |
from langchain_core.output_parsers import JsonOutputParser | |
class DaleAgent: | |
def __init__(self, name="Dale"): | |
self.name = name | |
self.client = openai.OpenAI( | |
api_key="LAAS", | |
base_url="https://api-laas.wanted.co.kr/api/preset/", | |
) | |
def get_prompt(self, query: str): | |
prompt_template = """๋น์ ์ ์๋ฌด๋ Dale Carnegie's book "How to Win Friends and Influence People"์์ ์ฌ์ฉ์์ ์ง๋ฌธ๊ณผ ๊ด๋ จ๋ ๋ด์ฉ์ ์ฐพ๋ ๊ฒ์ ๋๋ค. | |
์ด ์์ ์ ์ ๊ทผํ๋ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค: | |
1. ๋จผ์ ์ฌ์ฉ์์ ์ง๋ฌธ์ ์ฃผ์ ๊น๊ฒ ์ฝ๊ณ ์ดํดํฉ๋๋ค: | |
<user_question> | |
{USER_QUESTION} | |
</user_question> | |
2. ์ฌ์ฉ์์ ์ง๋ฌธ์ ๋ฐ๋ผ ๋ฐ์ผ ์นด๋ค๊ธฐ์ ์ฑ ์ ๋์ค๋ ์ด๋ค ์์น์ด๋ ๊ฐ๋ ์ด ๊ด๋ จ์ฑ์ด ์์์ง ์๊ฐํด ๋ณด์ธ์. ๋ค์๊ณผ ๊ฐ์ ์ฃผ์ ๋ฅผ ์๊ฐํด ๋ณด์ธ์: | |
- ๋ค๋ฅธ ์ฌ๋์๊ฒ ์ง์ ํ ๊ด์ฌ ๋ณด์ฌ์ฃผ๊ธฐ | |
- ์ข์ ๊ฒฝ์ฒญ์ ๋๊ธฐ | |
- ๋ค๋ฅธ ์ฌ๋์ด ์ค์ํ๋ค๊ณ ๋๋ผ๋๋ก ํ๊ธฐ | |
- ๋นํ๊ณผ ๋ถํ ํผํ๊ธฐ | |
- ์์งํ๊ณ ์ง์ฌ ์ด๋ฆฐ ๊ฐ์ฌ ํํํ๊ธฐ | |
- ์๋๋ฐฉ์ ๊ด์ ์์ ์ฌ๋ฌผ์ ๋ฐ๋ผ๋ณด๊ธฐ | |
- ๋ค๋ฅธ ์ฌ๋์ ๊ฒฉ๋ คํ๊ณ ๊ทธ๋ค์ ์๋ชป์ ์ฝ๊ฒ ๊ณ ์น ์ ์๋๋ก ๋ง๋ค๊ธฐ | |
- ์นญ์ฐฌ๊ณผ ์ ์งํ ๊ฐ์ฌ๋ฅผ ์ฌ์ฉํ๊ธฐ | |
3. ์นด๋ค๊ธฐ์ ์ฑ ์์ ๊ด๋ จ ์์น์ ๋ณธ์ง์ ํฌ์ฐฉํ ์ ์๋ ํค์๋๋ ๋ฌธ๊ตฌ๋ฅผ 2~3๊ฐ ์์ฑํด ๋ณด์ธ์. ์ด ๋ฌธ์ฅ์ผ๋ก ์นด๋ค๊ธฐ์ ์ ์์์ ๊ด๋ จ๋ ๋ด์ฉ์ ์ฐพ์ ์ ์์ต๋๋ค. | |
4. ์๋ต์ ์์ฑํ ๋๋ ๋ค์ ์ฌํญ์ ๊ณ ๋ คํด ์ฃผ์ธ์: | |
- ์์ด๋ก ์์ฑํด ์ฃผ์ธ์. | |
- ์ ์์ ์๋ฌธ์ ์ธ์ฉํ๊ฑฐ๋ ์์ฝํ์ฌ ์ฌ์ฉํด ์ฃผ์ธ์. | |
- ๊ฐ๋ฅํ ํ ๊ฐ๊ฒฐํ๊ฒ ์์ฑํด ์ฃผ์ธ์. | |
- ๋ฐฐ์ด๋ก ์์ฑํด ์ฃผ์ธ์. | |
#Format: Return a JSON array. | |
[ | |
"The only way to get the best of an argument is to avoid it.", | |
"If you want to gather honey, don't kick over the beehive.", | |
"Criticism is futile because it puts a person on the defensive and usually makes them strive to justify themselves." | |
] | |
""" | |
prompt = PromptTemplate.from_template(prompt_template) | |
return prompt.format(USER_QUESTION=query) | |
def get_chat_completions(self, query: str): | |
response = self.client.chat.completions.create( | |
extra_headers={ | |
"apiKey": os.environ.get("LAAS_API_KEY"), | |
"project": os.environ.get("LAAS_PROJECT"), | |
}, | |
extra_body={ | |
"hash": "d33f1c5fe1f4c091a88357b61f8638599b5556445454a87ef0a1a6d35846fe0d", | |
}, | |
model="gpt-4o", | |
messages=[ | |
{ | |
"role": "system", | |
"content": self.get_prompt(query), | |
} | |
], | |
) | |
return AIMessage(content=response.choices[0].message.content) | |
def get_sentence_completions(self, query: str): | |
response = self.get_chat_completions(query) | |
texts = JsonOutputParser().parse(text=response.content) | |
filtered_texts = [text for text in texts if len(text) >= 10] | |
return filtered_texts | |
if __name__ == "__main__": | |
query = "How can I make friends and influence people?" | |
response = DaleAgent().get_sentence_completions(query) | |
print(response) | |