CareerAdvisePro / agents /dale_agent.py
jeongsk's picture
.
7f276ad
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)