jeongsk commited on
Commit
7f276ad
ยท
1 Parent(s): 26dc1ed
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