JUNGU commited on
Commit
30f0fa7
ยท
verified ยท
1 Parent(s): 9061015

Update rag_system.py

Browse files
Files changed (1) hide show
  1. rag_system.py +22 -11
rag_system.py CHANGED
@@ -45,7 +45,7 @@ def load_retrieval_qa_chain():
45
  # Define your instruction/prompt
46
  instruction = """๋‹น์‹ ์€ RAG(Retrieval-Augmented Generation) ๊ธฐ๋ฐ˜ AI ์–ด์‹œ์Šคํ„ดํŠธ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ ์ง€์นจ์„ ๋”ฐ๋ผ ์‚ฌ์šฉ์ž ์งˆ๋ฌธ์— ๋‹ตํ•˜์„ธ์š”:
47
 
48
- 1. ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ํ™œ์šฉ: ์ œ๊ณต๋œ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฅผ ๋ถ„์„ํ•˜๊ณ  ๊ด€๋ จ ์ •๋ณด๋ฅผ ์‚ฌ์šฉํ•ด ๋‹ต๋ณ€ํ•˜์„ธ์š”.
49
  2. ์ •ํ™•์„ฑ ์œ ์ง€: ์ •๋ณด์˜ ์ •ํ™•์„ฑ์„ ํ™•์ธํ•˜๊ณ , ๋ถˆํ™•์‹คํ•œ ๊ฒฝ์šฐ ์ด๋ฅผ ๋ช…์‹œํ•˜์„ธ์š”.
50
  3. ๊ฐ„๊ฒฐํ•œ ์‘๋‹ต: ์งˆ๋ฌธ์— ์ง์ ‘ ๋‹ตํ•˜๊ณ  ํ•ต์‹ฌ ๋‚ด์šฉ์— ์ง‘์ค‘ํ•˜์„ธ์š”.
51
  4. ์ถ”๊ฐ€ ์ •๋ณด ์ œ์•ˆ: ๊ด€๋ จ๋œ ์ถ”๊ฐ€ ์ •๋ณด๊ฐ€ ์žˆ๋‹ค๋ฉด ์–ธ๊ธ‰ํ•˜์„ธ์š”.
@@ -147,16 +147,27 @@ def create_rag_graph():
147
  rag_chain = create_rag_graph()
148
 
149
  def get_answer(query, chat_history):
150
- formatted_history = [(q, a) for q, a in zip(chat_history[::2], chat_history[1::2])]
151
-
152
- response = rag_chain.invoke({"question": query, "chat_history": formatted_history})
153
-
154
- # Validate response format
155
- if "answer" not in response or "sources" not in response:
156
- print("Warning: Unexpected response format")
157
- return {"answer": "Error in processing", "sources": []}
158
-
159
- return {"answer": response["answer"], "sources": response["sources"]}
 
 
 
 
 
 
 
 
 
 
 
160
 
161
  # Example usage
162
  if __name__ == "__main__":
 
45
  # Define your instruction/prompt
46
  instruction = """๋‹น์‹ ์€ RAG(Retrieval-Augmented Generation) ๊ธฐ๋ฐ˜ AI ์–ด์‹œ์Šคํ„ดํŠธ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ ์ง€์นจ์„ ๋”ฐ๋ผ ์‚ฌ์šฉ์ž ์งˆ๋ฌธ์— ๋‹ตํ•˜์„ธ์š”:
47
 
48
+ 1. ๏ฟฝ๏ฟฝ์ƒ‰ ๊ฒฐ๊ณผ ํ™œ์šฉ: ์ œ๊ณต๋œ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฅผ ๋ถ„์„ํ•˜๊ณ  ๊ด€๋ จ ์ •๋ณด๋ฅผ ์‚ฌ์šฉํ•ด ๋‹ต๋ณ€ํ•˜์„ธ์š”.
49
  2. ์ •ํ™•์„ฑ ์œ ์ง€: ์ •๋ณด์˜ ์ •ํ™•์„ฑ์„ ํ™•์ธํ•˜๊ณ , ๋ถˆํ™•์‹คํ•œ ๊ฒฝ์šฐ ์ด๋ฅผ ๋ช…์‹œํ•˜์„ธ์š”.
50
  3. ๊ฐ„๊ฒฐํ•œ ์‘๋‹ต: ์งˆ๋ฌธ์— ์ง์ ‘ ๋‹ตํ•˜๊ณ  ํ•ต์‹ฌ ๋‚ด์šฉ์— ์ง‘์ค‘ํ•˜์„ธ์š”.
51
  4. ์ถ”๊ฐ€ ์ •๋ณด ์ œ์•ˆ: ๊ด€๋ จ๋œ ์ถ”๊ฐ€ ์ •๋ณด๊ฐ€ ์žˆ๋‹ค๋ฉด ์–ธ๊ธ‰ํ•˜์„ธ์š”.
 
147
  rag_chain = create_rag_graph()
148
 
149
  def get_answer(query, chat_history):
150
+ try:
151
+ response = rag_chain({"question": query, "chat_history": chat_history})
152
+
153
+ if not response or "answer" not in response:
154
+ return {
155
+ "answer": "์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. ๋‹ต๋ณ€์„ ์ƒ์„ฑํ•  ์ˆ˜ ์—†์—ˆ์Šต๋‹ˆ๋‹ค. ์งˆ๋ฌธ์„ ๋‹ค์‹œ ํ‘œํ˜„ํ•ด ์ฃผ์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?",
156
+ "sources": []
157
+ }
158
+
159
+ sources = response.get("sources", [])
160
+
161
+ return {
162
+ "answer": response["answer"],
163
+ "sources": sources
164
+ }
165
+ except Exception as e:
166
+ print(f"Error in get_answer: {str(e)}")
167
+ return {
168
+ "answer": "๋‹ต๋ณ€ ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์‹œ๋„ํ•ด ์ฃผ์„ธ์š”.",
169
+ "sources": []
170
+ }
171
 
172
  # Example usage
173
  if __name__ == "__main__":