Spaces:
Sleeping
Sleeping
- agents/__init__.py +0 -0
- agents/dale_agent.py +89 -0
- dale_retriver_agent.ipynb +0 -0
agents/__init__.py
ADDED
File without changes
|
agents/dale_agent.py
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from dotenv import load_dotenv
|
2 |
+
|
3 |
+
_ = load_dotenv()
|
4 |
+
|
5 |
+
import os
|
6 |
+
import openai
|
7 |
+
from langchain_core.messages import AIMessage
|
8 |
+
from langchain_core.prompts import PromptTemplate
|
9 |
+
from langchain_core.output_parsers import JsonOutputParser
|
10 |
+
|
11 |
+
|
12 |
+
class DaleAgent:
|
13 |
+
def __init__(self, name="Dale"):
|
14 |
+
self.name = name
|
15 |
+
self.client = openai.OpenAI(
|
16 |
+
api_key="LAAS",
|
17 |
+
base_url="https://api-laas.wanted.co.kr/api/preset/",
|
18 |
+
)
|
19 |
+
|
20 |
+
def get_prompt(self, query: str):
|
21 |
+
prompt_template = """๋น์ ์ ์๋ฌด๋ Dale Carnegie's book "How to Win Friends and Influence People"์์ ์ฌ์ฉ์์ ์ง๋ฌธ๊ณผ ๊ด๋ จ๋ ๋ด์ฉ์ ์ฐพ๋ ๊ฒ์
๋๋ค.
|
22 |
+
|
23 |
+
์ด ์์
์ ์ ๊ทผํ๋ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:
|
24 |
+
|
25 |
+
1. ๋จผ์ ์ฌ์ฉ์์ ์ง๋ฌธ์ ์ฃผ์ ๊น๊ฒ ์ฝ๊ณ ์ดํดํฉ๋๋ค:
|
26 |
+
<user_question>
|
27 |
+
{USER_QUESTION}
|
28 |
+
</user_question>
|
29 |
+
|
30 |
+
2. ์ฌ์ฉ์์ ์ง๋ฌธ์ ๋ฐ๋ผ ๋ฐ์ผ ์นด๋ค๊ธฐ์ ์ฑ
์ ๋์ค๋ ์ด๋ค ์์น์ด๋ ๊ฐ๋
์ด ๊ด๋ จ์ฑ์ด ์์์ง ์๊ฐํด ๋ณด์ธ์. ๋ค์๊ณผ ๊ฐ์ ์ฃผ์ ๋ฅผ ์๊ฐํด ๋ณด์ธ์:
|
31 |
+
- ๋ค๋ฅธ ์ฌ๋์๊ฒ ์ง์ ํ ๊ด์ฌ ๋ณด์ฌ์ฃผ๊ธฐ
|
32 |
+
- ์ข์ ๊ฒฝ์ฒญ์ ๋๊ธฐ
|
33 |
+
- ๋ค๋ฅธ ์ฌ๋์ด ์ค์ํ๋ค๊ณ ๋๋ผ๋๋ก ํ๊ธฐ
|
34 |
+
- ๋นํ๊ณผ ๋ถํ ํผํ๊ธฐ
|
35 |
+
- ์์งํ๊ณ ์ง์ฌ ์ด๋ฆฐ ๊ฐ์ฌ ํํํ๊ธฐ
|
36 |
+
- ์๋๋ฐฉ์ ๊ด์ ์์ ์ฌ๋ฌผ์ ๋ฐ๋ผ๋ณด๊ธฐ
|
37 |
+
- ๋ค๋ฅธ ์ฌ๋์ ๊ฒฉ๋ คํ๊ณ ๊ทธ๋ค์ ์๋ชป์ ์ฝ๊ฒ ๊ณ ์น ์ ์๋๋ก ๋ง๋ค๊ธฐ
|
38 |
+
- ์นญ์ฐฌ๊ณผ ์ ์งํ ๊ฐ์ฌ๋ฅผ ์ฌ์ฉํ๊ธฐ
|
39 |
+
|
40 |
+
3. ์นด๋ค๊ธฐ์ ์ฑ
์์ ๊ด๋ จ ์์น์ ๋ณธ์ง์ ํฌ์ฐฉํ ์ ์๋ ํค์๋๋ ๋ฌธ๊ตฌ๋ฅผ 2~3๊ฐ ์์ฑํด ๋ณด์ธ์. ์ด ๋ฌธ์ฅ์ผ๋ก ์นด๋ค๊ธฐ์ ์ ์์์ ๊ด๋ จ๋ ๋ด์ฉ์ ์ฐพ์ ์ ์์ต๋๋ค.
|
41 |
+
|
42 |
+
4. ์๋ต์ ์์ฑํ ๋๋ ๋ค์ ์ฌํญ์ ๊ณ ๋ คํด ์ฃผ์ธ์:
|
43 |
+
- ์์ด๋ก ์์ฑํด ์ฃผ์ธ์.
|
44 |
+
- ์ ์์ ์๋ฌธ์ ์ธ์ฉํ๊ฑฐ๋ ์์ฝํ์ฌ ์ฌ์ฉํด ์ฃผ์ธ์.
|
45 |
+
- ๊ฐ๋ฅํ ํ ๊ฐ๊ฒฐํ๊ฒ ์์ฑํด ์ฃผ์ธ์.
|
46 |
+
- ๋ฐฐ์ด๋ก ์์ฑํด ์ฃผ์ธ์.
|
47 |
+
|
48 |
+
#Format: Return a JSON array.
|
49 |
+
[
|
50 |
+
"The only way to get the best of an argument is to avoid it.",
|
51 |
+
"If you want to gather honey, don't kick over the beehive.",
|
52 |
+
"Criticism is futile because it puts a person on the defensive and usually makes them strive to justify themselves."
|
53 |
+
]
|
54 |
+
"""
|
55 |
+
|
56 |
+
prompt = PromptTemplate.from_template(prompt_template)
|
57 |
+
return prompt.format(USER_QUESTION=query)
|
58 |
+
|
59 |
+
def get_chat_completions(self, query: str):
|
60 |
+
response = self.client.chat.completions.create(
|
61 |
+
extra_headers={
|
62 |
+
"apiKey": os.environ.get("LAAS_API_KEY"),
|
63 |
+
"project": os.environ.get("LAAS_PROJECT"),
|
64 |
+
},
|
65 |
+
extra_body={
|
66 |
+
"hash": "d33f1c5fe1f4c091a88357b61f8638599b5556445454a87ef0a1a6d35846fe0d",
|
67 |
+
},
|
68 |
+
model="gpt-4o",
|
69 |
+
messages=[
|
70 |
+
{
|
71 |
+
"role": "system",
|
72 |
+
"content": self.get_prompt(query),
|
73 |
+
}
|
74 |
+
],
|
75 |
+
)
|
76 |
+
|
77 |
+
return AIMessage(content=response.choices[0].message.content)
|
78 |
+
|
79 |
+
def get_sentence_completions(self, query: str):
|
80 |
+
response = self.get_chat_completions(query)
|
81 |
+
texts = JsonOutputParser().parse(text=response.content)
|
82 |
+
filtered_texts = [text for text in texts if len(text) >= 10]
|
83 |
+
return filtered_texts
|
84 |
+
|
85 |
+
|
86 |
+
if __name__ == "__main__":
|
87 |
+
query = "How can I make friends and influence people?"
|
88 |
+
response = DaleAgent().get_sentence_completions(query)
|
89 |
+
print(response)
|
dale_retriver_agent.ipynb
CHANGED
The diff for this file is too large to render.
See raw diff
|
|