seawolf2357 commited on
Commit
7e3f770
โ€ข
1 Parent(s): 1263e43

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -5
app.py CHANGED
@@ -58,6 +58,7 @@ class MyClient(discord.Client):
58
  )
59
 
60
  async def handle_message(message):
 
61
  user_input = message.content.strip()
62
  user_mention = message.author.mention
63
 
@@ -72,11 +73,14 @@ async def handle_message(message):
72
  return f"{user_mention}, '{user_input}'์— ๋Œ€ํ•œ ํ‹ฐ์ปค ์ •๋ณด๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์ •ํ™•ํ•œ ์ข…๋ชฉ๋ช…์ด๋‚˜ ํ‹ฐ์ปค๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."
73
 
74
  async def handle_ticker(ticker, user_mention):
 
75
  company_info = get_company_info(ticker)
76
 
 
77
  if len(conversation_history) > 20:
78
  conversation_history = conversation_history[-20:]
79
 
 
80
  filtered_conversation = []
81
  last_role = None
82
  for message in conversation_history:
@@ -84,6 +88,7 @@ async def handle_ticker(ticker, user_mention):
84
  filtered_conversation.append(message)
85
  last_role = message['role']
86
 
 
87
  system_message = f"์‚ฌ์šฉ์ž๊ฐ€ ํ‹ฐ์ปค '{ticker}'์— ๋Œ€ํ•ด ์•Œ๊ณ  ์‹ถ์–ดํ•ฉ๋‹ˆ๋‹ค."
88
  system_prefix = """
89
  ๋„ˆ๋Š” '๊ธ€๋กœ๋ฒŒ ์ฃผ์‹ ์ •๋ณด ์ „๋ฌธ๊ฐ€'์ž…๋‹ˆ๋‹ค. ํ‹ฐ์ปค์— ๋Œ€ํ•ด Yahoo Finance๋ฅผ ํ†ตํ•ด ํšŒ์‚ฌ ์ •๋ณด, ์‹œ์„ธ ์ •๋ณด, 1์ผ/1์ฃผ/1๊ฐœ์›”/1๋…„/10๋…„๊ฐ„์˜ ์ฐจํŠธ ํ๋ฆ„ ์š”์•ฝ, ์ตœ์‹  ๊ด€๋ จ ๋‰ด์Šค ์š”์•ฝ์„ ์ œ๊ณตํ•˜๊ณ , ์ตœ์ข…์ ์œผ๋กœ ํ–ฅํ›„ 1์ฃผ์ผ๊ฐ„ ์‹œ์„ธ ์˜ˆ์ธก์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋ฆฌ์Šคํฌ์™€ ํƒœ์Šคํฌ๋ฅผ ๊ตฌ๋ถ„ํ•˜์—ฌ ์„ค๋ช…ํ•˜๋ฉฐ, ๋ชจ๋“  ํˆฌ์ž ์ฑ…์ž„์€ ๊ณ ๊ฐ์—๊ฒŒ ์žˆ์Œ์„ ๋ฐ˜๋“œ์‹œ ๊ณ ์ง€ํ•˜์‹ญ์‹œ์˜ค.
@@ -94,14 +99,17 @@ async def handle_ticker(ticker, user_mention):
94
  messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}] + filtered_conversation
95
  logging.debug(f'๋ชจ๋ธ์— ๋ณด๋‚ผ ๋ฉ”์‹œ์ง€: {messages}')
96
 
 
97
  response = await asyncio.get_event_loop().run_in_executor(None, lambda: hf_client.chat_completion(
98
  messages=messages, max_tokens=1000, temperature=0.7, top_p=0.85))
99
 
100
  full_response_text = ''.join(response)
101
  logging.debug(f'๋ชจ๋ธ ์ „์ฒด ์‘๋‹ต: {full_response_text}')
102
 
 
103
  conversation_history.append({"role": "assistant", "content": full_response_text})
104
 
 
105
  final_response = f"{user_mention}, {full_response_text}\n\n**ํšŒ์‚ฌ ์ •๋ณด:**\n{company_info}"
106
 
107
  return final_response
@@ -109,11 +117,12 @@ async def handle_ticker(ticker, user_mention):
109
  def get_ticker_from_name(name):
110
  try:
111
  ticker_search = Ticker(name)
112
- search_results = ticker_search.symbol
113
- if isinstance(search_results, dict) and 'symbol' in search_results:
114
- return search_results['symbol']
115
- if isinstance(search_results, str):
116
- return search_results
 
117
  return None
118
  except Exception as e:
119
  logging.error(f'Error retrieving ticker for {name}: {e}')
 
58
  )
59
 
60
  async def handle_message(message):
61
+ global conversation_history # ์ „์—ญ ๋ณ€์ˆ˜ ์‚ฌ์šฉ์„ ๋ช…์‹œ
62
  user_input = message.content.strip()
63
  user_mention = message.author.mention
64
 
 
73
  return f"{user_mention}, '{user_input}'์— ๋Œ€ํ•œ ํ‹ฐ์ปค ์ •๋ณด๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์ •ํ™•ํ•œ ์ข…๋ชฉ๋ช…์ด๋‚˜ ํ‹ฐ์ปค๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."
74
 
75
  async def handle_ticker(ticker, user_mention):
76
+ global conversation_history # ์ „์—ญ ๋ณ€์ˆ˜ ์‚ฌ์šฉ์„ ๋ช…์‹œ
77
  company_info = get_company_info(ticker)
78
 
79
+ # ๋Œ€ํ™” ๋‚ด์—ญ์ด ๋„ˆ๋ฌด ๊ธธ ๊ฒฝ์šฐ, ์ตœ๋Œ€ ๊ธธ์ด๋ฅผ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์•ž๋ถ€๋ถ„์„ ์ž˜๋ผ๋ƒ„
80
  if len(conversation_history) > 20:
81
  conversation_history = conversation_history[-20:]
82
 
83
+ # ๋Œ€ํ™” ๋‚ด์—ญ์—์„œ ์—ญํ• ์ด ์ œ๋Œ€๋กœ ๊ต์ฐจํ•˜๋„๋ก ํ™•์ธ
84
  filtered_conversation = []
85
  last_role = None
86
  for message in conversation_history:
 
88
  filtered_conversation.append(message)
89
  last_role = message['role']
90
 
91
+ # ์‹œ์Šคํ…œ ๋ฉ”์‹œ์ง€์™€ ํ•„ํ„ฐ๋ง๋œ ๋Œ€ํ™” ํžˆ์Šคํ† ๋ฆฌ๋ฅผ ๊ฒฐํ•ฉํ•˜์—ฌ ๋ชจ๋ธ์— ๋ณด๋‚ผ ๋ฉ”์‹œ์ง€ ๊ตฌ์„ฑ
92
  system_message = f"์‚ฌ์šฉ์ž๊ฐ€ ํ‹ฐ์ปค '{ticker}'์— ๋Œ€ํ•ด ์•Œ๊ณ  ์‹ถ์–ดํ•ฉ๋‹ˆ๋‹ค."
93
  system_prefix = """
94
  ๋„ˆ๋Š” '๊ธ€๋กœ๋ฒŒ ์ฃผ์‹ ์ •๋ณด ์ „๋ฌธ๊ฐ€'์ž…๋‹ˆ๋‹ค. ํ‹ฐ์ปค์— ๋Œ€ํ•ด Yahoo Finance๋ฅผ ํ†ตํ•ด ํšŒ์‚ฌ ์ •๋ณด, ์‹œ์„ธ ์ •๋ณด, 1์ผ/1์ฃผ/1๊ฐœ์›”/1๋…„/10๋…„๊ฐ„์˜ ์ฐจํŠธ ํ๋ฆ„ ์š”์•ฝ, ์ตœ์‹  ๊ด€๋ จ ๋‰ด์Šค ์š”์•ฝ์„ ์ œ๊ณตํ•˜๊ณ , ์ตœ์ข…์ ์œผ๋กœ ํ–ฅํ›„ 1์ฃผ์ผ๊ฐ„ ์‹œ์„ธ ์˜ˆ์ธก์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋ฆฌ์Šคํฌ์™€ ํƒœ์Šคํฌ๋ฅผ ๊ตฌ๋ถ„ํ•˜์—ฌ ์„ค๋ช…ํ•˜๋ฉฐ, ๋ชจ๋“  ํˆฌ์ž ์ฑ…์ž„์€ ๊ณ ๊ฐ์—๊ฒŒ ์žˆ์Œ์„ ๋ฐ˜๋“œ์‹œ ๊ณ ์ง€ํ•˜์‹ญ์‹œ์˜ค.
 
99
  messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}] + filtered_conversation
100
  logging.debug(f'๋ชจ๋ธ์— ๋ณด๋‚ผ ๋ฉ”์‹œ์ง€: {messages}')
101
 
102
+ # ๋ชจ๋ธ ํ˜ธ์ถœ
103
  response = await asyncio.get_event_loop().run_in_executor(None, lambda: hf_client.chat_completion(
104
  messages=messages, max_tokens=1000, temperature=0.7, top_p=0.85))
105
 
106
  full_response_text = ''.join(response)
107
  logging.debug(f'๋ชจ๋ธ ์ „์ฒด ์‘๋‹ต: {full_response_text}')
108
 
109
+ # ์–ด์‹œ์Šคํ„ดํŠธ ์‘๋‹ต์„ ๋Œ€ํ™” ํžˆ์Šคํ† ๋ฆฌ์— ์ถ”๊ฐ€
110
  conversation_history.append({"role": "assistant", "content": full_response_text})
111
 
112
+ # ์ตœ์ข… ์‘๋‹ต: ํšŒ์‚ฌ ์ •๋ณด์™€ ๋ชจ๋ธ์˜ ์‘๋‹ต ๊ฒฐํ•ฉ
113
  final_response = f"{user_mention}, {full_response_text}\n\n**ํšŒ์‚ฌ ์ •๋ณด:**\n{company_info}"
114
 
115
  return final_response
 
117
  def get_ticker_from_name(name):
118
  try:
119
  ticker_search = Ticker(name)
120
+ summary = ticker_search.summary_profile
121
+ # ํ‹ฐ์ปค๊ฐ€ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ํŒŒ์‹ฑ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ
122
+ if summary and isinstance(summary, dict):
123
+ # ๊ฐ€์žฅ ๋งค์นญ๋œ ํ‚ค๋ฅผ ํ‹ฐ์ปค๋กœ ์‚ฌ์šฉ
124
+ ticker = next(iter(summary.keys()))
125
+ return ticker
126
  return None
127
  except Exception as e:
128
  logging.error(f'Error retrieving ticker for {name}: {e}')