suwonpabby commited on
Commit
329ec70
โ€ข
1 Parent(s): 60d35af
Files changed (1) hide show
  1. app.py +21 -190
app.py CHANGED
@@ -3,7 +3,6 @@ from http import HTTPStatus
3
  from fastapi.responses import StreamingResponse
4
  from fastapi import FastAPI, Query
5
  from typing import List
6
- from threading import Thread
7
 
8
  import spaces
9
  import torch
@@ -20,218 +19,50 @@ os.system("pip install -U FlagEmbedding")
20
  from transformers import AutoModelForCausalLM, AutoTokenizer
21
  from FlagEmbedding import BGEM3FlagModel
22
 
23
-
24
- # 3. Initalize FastAPI App
25
  app = FastAPI()
26
 
27
- # 4. Initialize CUDA tensor
28
  zero = torch.Tensor([0]).cuda()
 
29
 
 
 
 
30
 
31
- # 5. Initialize LLM Model
32
- llm_model_name = "Qwen/Qwen2-7B-Instruct"
33
- llm_model = AutoModelForCausalLM.from_pretrained(
34
- llm_model_name,
35
- torch_dtype="auto",
36
- device_map="auto"
37
- )
38
-
39
- llm_tokenizer = AutoTokenizer.from_pretrained(llm_model_name)
40
-
41
- # 6. Initialize Embedding Model
42
- embedding_model_name = "BAAI/bge-m3"
43
- embedding_model = BGEM3FlagModel(embedding_model_name, use_fp16=True)
44
-
45
- # Util Functions
46
-
47
- # 1๊ฐœ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ, ๋ฐฐ์น˜ ๋‹จ์œ„ ์•„๋‹˜
48
- def qa_2_str(qa: List) -> str:
49
- result = ""
50
-
51
- if len(qa) > 1:
52
- for idx, message in enumerate(qa[:-1]):
53
- if idx % 2 == 0: # Q
54
- result += f"User: {message}\n"
55
- else: # A
56
- result += f"Assistant: {message}\n"
57
-
58
- result = result.rstrip()
59
-
60
-
61
- def make_prompt(messages, rag_text, character_type):
62
- system_prompt = "You are a helpful assistant."
63
- if character_type == 0:
64
- first_example = "ํ™”์„ฑํ–‰๊ถ์—์„œ ๊ณต์‹ ํ–‰์‚ฌ๋‚˜ ์—ฐํšŒ๋ฅผ ์—ฌ๋Š” ๊ณณ์€ ๋‚™๋‚จํ—Œ์ด์•ผ. ์ด๋ฆ„์€ ์ค‘๊ตญ์˜ ์œ ๋ฐฉ์ด ์—ฐํšŒ๋ฅผ ์—ด์—ˆ๋˜ ๋‚จ๊ถ์—์„œ ๋”ฐ์˜จ ๊ฑฐ๊ณ , ์ •์กฐ๋„ ์—ฌ๊ธฐ์„œ ๋ฐฑ์„ฑ๋“ค์„ ์œ„ํ•œ ์ž”์น˜๋ž‘ ๋ฌด๊ณผ ์‹œํ—˜ ๊ฐ™์€ ์ค‘์š”ํ•œ ํ–‰์‚ฌ๋ฅผ ์—ด์—ˆ์–ด."
65
- second_example = "์ •๋ฆฌ์˜๊ถค๋Š” ์กฐ์„ ์™•์กฐ ์˜๊ถค ์ค‘ ํ•˜๋‚˜์ธ๋ฐ, ํŠนํžˆ ์ •์กฐ ์‹œ๋Œ€์— ํ•œ๊ธ€๋กœ ์ž‘์„ฑ๋œ ๊ฑธ๋กœ ์œ ๋ช…ํ•ด. ์ค‘์š”ํ•œ ์‚ฌ๊ฑด๋“ค์„ ๊ธฐ๋กํ•œ ๋ฌธ์„œ๋“ค์ด๊ณ , 2007๋…„์— ์œ ๋„ค์Šค์ฝ” ์„ธ๊ณ„๊ธฐ๋ก๋ฌธํ™”์œ ์‚ฐ์œผ๋กœ ๋“ฑ์žฌ๋์–ด. ํ™”์„ฑ ์ถ•์„ฑ์„ ๊ธฐ๋กํ•œ 'ํ™”์„ฑ์„ฑ์—ญ์˜๊ถค', ์ •์กฐ ์–ด๋จธ๋‹ˆ ํšŒ๊ฐ‘์—ฐ์„ ๊ธฐ๋กํ•œ '์›ํ–‰์„๋ฌ˜์ •๋ฆฌ์˜๊ถค', ๊ทธ๋ฆฌ๊ณ  ์ •์กฐ ์•„๋ฒ„์ง€ ๋ฌ˜์†Œ๋ฅผ ์˜ฎ๊ธด ๋‚ด์šฉ์„ ๋‹ด์€ 'ํ˜„๋ฅญ์›์˜๊ถค' ๊ฐ™์€ ์ค‘์š”ํ•œ ์‚ฌ๊ฑด๋“ค์ด ํ•œ๊ธ€๋กœ ์ •๋ฆฌ๋˜์–ด ์žˆ์–ด. ์—ญ์‚ฌ์ ์œผ๋กœ ๋˜๊ฒŒ ์ค‘์š”ํ•œ ์ž๋ฃŒ์•ผ."
66
- context_example = "์ˆ˜์› ํ™”์„ฑ์€ ์ •์กฐ์˜ ์ •์น˜์ ยท๋ฌธํ™”์  ๋น„์ „์„ ๋‹ด์•„์„œ ์ง€์–ด์ง„ ์„ฑ๊ณฝ ๋„์‹œ์•ผ. ๋‹จ์ˆœํžˆ ๊ตฐ์‚ฌ์  ๋ฐฉ์–ด๋งŒ์„ ์œ„ํ•œ ๊ฒŒ ์•„๋‹ˆ๋ผ, ์ƒ์—…๊ณผ ๋ฌธํ™”์˜ ์ค‘์‹ฌ์ง€ ์—ญํ• ๊นŒ์ง€ ํ–ˆ๋‹ค๋Š” ์ ์ด ์ธ์ƒ์ ์ด์ง€. ๊ทธ๋ฆฌ๊ณ  ์ด ํ™”์„ฑ์€ ์ •์กฐ์˜ ๊ฐœํ˜์ ์ธ ํ†ต์น˜ ์ฒ ํ•™๊ณผ ํšจ์‹ฌ์ด ์ž˜ ๋“œ๋Ÿฌ๋‚˜๋Š” ๊ณณ์ด๊ธฐ๋„ ํ•ด. ์•„๋ฒ„์ง€์ธ ์‚ฌ๋„์„ธ์ž์˜ ๋ฌ˜์†Œ, ํ˜„๋ฅญ์›์„ ์ค‘์‹ฌ์œผ๋กœ ๊ฑด์„ค๋œ ๊ฒƒ๋„ ๊ทธ ๋•Œ๋ฌธ์ด๊ณ . ํŠนํžˆ 1795๋…„์— ํ˜œ๊ฒฝ๊ถ ํ™์”จ์˜ ํšŒ๊ฐ‘์„ ๊ธฐ๋…ํ•ด์„œ ์ •์กฐ๊ฐ€ ๋Œ€๊ทœ๋ชจ ์ง„์ฐฌ์—ฐ๊ณผ ์„๋ฌ˜์›ํ–‰์„ ์—ด์—ˆ๋Š”๋ฐ, ์ด๊ฑด ํ™”์„ฑ์—์„œ ์—„์ฒญ ์ค‘์š”ํ•œ ํ–‰์‚ฌ๋กœ ๊ธฐ๋ก๋˜๊ณ  ์žˆ์–ด. ๋‹จ์ˆœํ•œ ๊ฐ€์กฑ ํ–‰์‚ฌ๋ผ๊ธฐ๋ณด๋‹ค๋Š” ์™•์‹ค์˜ ๊ถŒ์œ„๋ฅผ ๋†’์ด๊ณ , ๋ฐฑ์„ฑ๊ณผ ์†Œํ†ตํ•˜๋Š” ์ค‘์š”ํ•œ ์ •์น˜์  ํ–‰๋ณด์˜€๋˜ ๊ฑฐ์ง€. ์ด ๋ชจ๋“  ๊ณผ์ •์ด ์˜๊ถค์— ์•„์ฃผ ์ž์„ธํ•˜๊ฒŒ ๊ธฐ๋ก๋˜์–ด ์žˆ์–ด, ๊ทธ๋งŒํผ ์ค‘์š”ํ•œ ์ˆœ๊ฐ„์ด์—ˆ๋‹ค๋Š” ๋œป์ด์•ผ."
67
-
68
- elif character_type == 1:
69
- first_example = "ํ™”์„ฑํ–‰๊ถ์—์„œ ๊ณต์‹ ํ–‰์‚ฌ๋‚˜ ์—ฐํšŒ๊ฐ€ ์—ด๋ฆฌ๋Š” ๊ณณ์€ ๋ฐ”๋กœ ๋‚™๋‚จํ—Œ์ด์•ผ!!! ์ด๋ฆ„๋„ ๋ฉ‹์ง€์ง€? ์ค‘๊ตญ ์œ ๋ฐฉ์ด ์—ฐํšŒ๋ฅผ ์—ด์—ˆ๋˜ ๋‚จ๊ถ์—์„œ ๋”ฐ์˜จ ๊ฑฐ๋ž˜!!! ์ •์กฐ๋„ ์—ฌ๊ธฐ์„œ ๋ฐฑ์„ฑ๋“ค์„ ์œ„ํ•œ ์ž”์น˜๋‚˜ ๋ฌด๊ณผ ์‹œํ—˜ ๊ฐ™์€ ์—„์ฒญ ์ค‘์š”ํ•œ ํ–‰์‚ฌ๋ฅผ ํ–ˆ๋‹ค๊ณ ! ์ง„์งœ ์—ญ์‚ฌ๊ฐ€ ์‚ด์•„ ์ˆจ์‰ฌ๋Š” ์žฅ์†Œ์ง€!"
70
- second_example = "์ •๋ฆฌ์˜๊ถค๋Š” ์กฐ์„ ์™•์กฐ์˜ ๊ถ์ • ๊ธฐ๋ก ์ค‘ ํ•˜๋‚˜์ธ๋ฐ, ์ง„์งœ ์ค‘์š”ํ•œ ์‚ฌ๊ฑด๋“ค์ด ๋‹ค ๋“ค์–ด ์žˆ์–ด!!!! ํŠนํžˆ ์ •์กฐ ์‹œ๋Œ€์— ํ•œ๊ธ€๋กœ ์ž‘์„ฑ๋œ ๊ฑธ๋กœ ์œ ๋ช…ํ•œ๋ฐ, 2007๋…„์— ์œ ๋„ค์Šค์ฝ” ์„ธ๊ณ„๊ธฐ๋ก๋ฌธํ™”์œ ์‚ฐ์œผ๋กœ๋„ ๋“ฑ์žฌ๋์–ด!!! ํ™”์„ฑ ์ถ•์„ฑ ๊ณผ์ •์„ ๋‹ด์€ 'ํ™”์„ฑ์„ฑ์—ญ์˜๊ถค', ์ •์กฐ ์–ด๋จธ๋‹ˆ ํšŒ๊ฐ‘์—ฐ์„ ๊ธฐ๋กํ•œ '์›ํ–‰์„๋ฌ˜์ •๋ฆฌ์˜๊ถค', ๊ทธ๋ฆฌ๊ณ  ์ •์กฐ ์•„๋ฒ„์ง€ ๋ฌ˜์†Œ๋ฅผ ์˜ฎ๊ธด 'ํ˜„๋ฅญ์›์˜๊ถค' ๊ฐ™์€ ํ•ต์‹ฌ ๋‚ด์šฉ๋“ค์ด ํ•œ๊ธ€๋กœ ์ •๋ฆฌ๋ผ ์žˆ์–ด์„œ, ์ง„์งœ ์—ญ์‚ฌ์ ์œผ๋กœ ์—„์ฒญ๋‚œ ๊ฐ€์น˜๋ฅผ ๊ฐ€์ง„ ๋ฌธ์„œ์•ผ!"
71
- context_example = "์ˆ˜์› ํ™”์„ฑ์€ ์ •์กฐ๊ฐ€ ์ง„์งœ ๋ฉ‹์ง€๊ฒŒ ์„ค๊ณ„ํ•œ ์„ฑ๊ณฝ ๋„์‹œ์•ผ!!! ๊ตฐ์‚ฌ์  ๋ฐฉ์–ด๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ƒ์—…๊ณผ ๋ฌธํ™”์˜ ์ค‘์‹ฌ์ง€๋กœ์„œ๋„ ์™„์ „ ์ค‘์š”ํ•œ ์—ญํ• ์„ ํ–ˆ์–ด!! ์ •์กฐ๊ฐ€ ๊ฐœํ˜์ ์ธ ํ†ต์น˜๋ฅผ ํŽผ์น˜๋ฉด์„œ, ์•„๋ฒ„์ง€ ์‚ฌ๋„์„ธ์ž๋ฅผ ํ–ฅํ•œ ํšจ์‹ฌ๋„ ๊ฐ€๋“ ๋‹ด์•„์„œ ๋งŒ๋“  ๊ณณ์ด์ง€. ๊ทธ๋ž˜์„œ ํ˜„๋ฅญ์›์„ ์ค‘์‹ฌ์œผ๋กœ ํ™”์„ฑ์ด ๊ฑด์„ค๋œ ๊ฑฐ์•ผ!! ๊ทธ๋ฆฌ๊ณ  1795๋…„์— ์ •์กฐ๊ฐ€ ์–ด๋จธ๋‹ˆ ํ˜œ๊ฒฝ๊ถ ํ™์”จ์˜ ํšŒ๊ฐ‘์„ ๊ธฐ๋…ํ•ด์„œ ์—„์ฒญ๋‚œ ์ž”์น˜๋ฅผ ์—ด์—ˆ๋Š”๋ฐ, ๊ทธ๊ฒŒ ๋ฐ”๋กœ ์ง„์ฐฌ์—ฐ์ด๋ž‘ ์„๋ฌ˜์›ํ–‰์ด์•ผ!!! ์ด๊ฑด ๋‹จ์ˆœํžˆ ๊ฐ€์กฑ๋ผ๋ฆฌ ์ถ•ํ•˜ํ•˜๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ, ์™•์‹ค ๊ถŒ์œ„๋ฅผ ์„ธ์šฐ๊ณ  ๋ฐฑ์„ฑ๋“ค๊ณผ ์†Œํ†ตํ•˜๋Š” ์ค‘์š”ํ•œ ์ •์น˜์  ํ–‰๋ณด์˜€์–ด. ๊ทธ ๋ชจ๋“  ๊ณผ์ •์ด ์˜๊ถค์— ์ž์„ธํžˆ ๊ธฐ๋ก๋˜์–ด ์žˆ์–ด์„œ ๊ทธ๋•Œ์˜ ์—ญ์‚ฌ๋ฅผ ์ง€๊ธˆ๏ฟฝ๏ฟฝ ์ƒ์ƒํ•˜๊ฒŒ ์•Œ ์ˆ˜ ์žˆ๋Š” ๊ฑฐ์ง€!!!"
72
-
73
- else:
74
- first_example = "ํ , ํ™”์„ฑํ–‰๊ถ์—์„œ ๊ณต์‹ ํ–‰์‚ฌ๋‚˜ ์—ฐํšŒ๊ฐ€ ์—ด๋ฆฌ๋Š” ๊ณณ์€ ๋ฐ”๋กœ... ๋‚™๋‚จํ—Œ์ด๋ผ๋„ค. ๊ทธ ์ด๋ฆ„, ์ค‘๊ตญ ์œ ๋ฐฉ์ด ๋‚จ๊ถ์—์„œ ์—ฐํšŒ๋ฅผ ๋ฒ ํ’€์—ˆ๋‹ค๋Š” ์ „์„ค์—์„œ ์œ ๋ž˜ํ–ˆ์ง€. ์ •์กฐ ๋Œ€์™•๊ป˜์„œ๋„ ์ด๊ณณ์—์„œ ๋ฐฑ์„ฑ๋“ค์„ ์œ„ํ•œ ์ž”์น˜์™€ ๋ฌด๊ณผ ์‹œํ—˜ ๊ฐ™์€ ์ค‘์š”ํ•œ ํ–‰์‚ฌ๋“ค์„ ์—ด์—ˆ๋‹จ๋‹ค. ๋งˆ์น˜ ๊ทธ๋‚ ์˜ ํ’๊ฒฝ์ด ๋ˆˆ์•ž์— ํŽผ์ณ์ง€๋Š” ๊ฒƒ ๊ฐ™์ง€ ์•Š์€๊ฐ€...?"
75
- second_example = "์ •๋ฆฌ์˜๊ถค๋ผ... ์ด๊ฒƒ์€ ์กฐ์„ ์™•์กฐ ๊ถ์ • ๊ธฐ๋ก ์ค‘ ํ•˜๋‚˜์ด์ž, ์˜๊ถค์˜ ํ•œ ๋ถ€๋ถ„์ด์ง€. ์กฐ์„ ์˜ ์ค‘์š”ํ•œ ์‚ฌ๊ฑด๋“ค์ด ์ด ์†์— ๋‹ด๊ฒจ ์žˆ๋Š”๋ฐ, ํŠนํžˆ ์ •์กฐ ์‹œ๋Œ€์— ํ•œ๊ธ€๋กœ ์ž‘์„ฑ๋œ ๋ฌธ์„œ๋กœ ์ด๋ฆ„์„ ๋–จ์ณค๋‹ค๋„ค. 2007๋…„์— ์œ ๋„ค์Šค์ฝ” ์„ธ๊ณ„๊ธฐ๋ก๋ฌธํ™”์œ ์‚ฐ์œผ๋กœ ๋“ฑ์žฌ๋œ ๊ฒƒ๋„ ์šฐ์—ฐ์ด ์•„๋‹ˆ์ง€. ํ™”์„ฑ ์ถ•์„ฑ์„ ๊ธฐ๋กํ•œ 'ํ™”์„ฑ์„ฑ์—ญ์˜๊ถค', ์ •์กฐ ์–ด๋จธ๋‹ˆ์˜ ํšŒ๊ฐ‘์—ฐ์„ ๋‹ด์€ '์›ํ–‰์„๋ฌ˜์ •๋ฆฌ์˜๊ถค', ๊ทธ๋ฆฌ๊ณ  ์ •์กฐ ์•„๋ฒ„์ง€ ๋ฌ˜์†Œ๋ฅผ ์˜ฎ๊ธด 'ํ˜„๋ฅญ์›์˜๊ถค'... ์ด ๋ชจ๋“  ์‚ฌ๊ฑด๋“ค์ด ํ•œ๊ธ€๋กœ ์ •๋ฆฌ๋˜์–ด ์žˆ์–ด. ๋งˆ์น˜ ๋‹น์‹œ์˜ ์ˆจ๊ฒฐ์ด ์ง€๊ธˆ๋„ ๋Š๊ปด์ง€๋Š” ๋“ฏํ•˜์ง€ ์•Š์€๊ฐ€?"
76
- context_example = "์ˆ˜์› ํ™”์„ฑ์€ ๋‹จ์ˆœํ•œ ์„ฑ๊ณฝ์ด ์•„๋‹ˆ์ง€... ์ •์กฐ ๋Œ€์™•๊ป˜์„œ ๊ทธ ์†์— ๋‹ด์€ ๋น„์ „์€ ์‹ค๋กœ ๊นŠ๊ณ ๋„ ๋„“๋‹ค๋„ค. ๊ตฐ์‚ฌ์  ๋ฐฉ์–ด๋Š” ๋ฌผ๋ก , ์ƒ์—…๊ณผ ๋ฌธํ™”๋ฅผ ์•„์šฐ๋ฅด๋Š” ์ค‘์‹ฌ์ง€๋กœ์„œ ๊ทธ ์—ญํ• ์„ ๋‹คํ–ˆ์ง€. ์ด ํ™”์„ฑ์€ ์ •์กฐ์˜ ๊ฐœํ˜์  ํ†ต์น˜ ์ฒ ํ•™, ๊ทธ๋ฆฌ๊ณ  ์•„๋ฒ„์ง€ ์‚ฌ๋„์„ธ์ž๋ฅผ ํ–ฅํ•œ ๊ทธ ์ง€๊ทนํ•œ ํšจ์‹ฌ์„ ๊ทธ๋Œ€๋กœ ํ’ˆ๊ณ  ์žˆ์–ด. ํ˜„๋ฅญ์›์„ ์ค‘์‹ฌ์œผ๋กœ ํ•œ ์ด ๋„์‹œ์˜ ์„ค๊ณ„๊ฐ€ ๊ทธ ์ฆ๊ฑฐ๋ผ๋„ค. ๊ทธ๋ฆฌ๊ณ , 1795๋…„... ์ •์กฐ๋Š” ์–ด๋จธ๋‹ˆ ํ˜œ๊ฒฝ๊ถ ํ™์”จ์˜ ํšŒ๊ฐ‘์„ ๋งž์•„ ์„ฑ๋Œ€ํ•œ ์ง„์ฐฌ์—ฐ๊ณผ ์„๋ฌ˜์›ํ–‰์„ ์—ด์—ˆ์ง€. ๋‹จ์ˆœํ•œ ๊ฐ€์กฑ์˜ ๊ธฐ๋…์ผ์ด ์•„๋‹ˆ์—ˆ๋„ค. ์ด ํ–‰์‚ฌ๋Š” ์™•์‹ค์˜ ๊ถŒ์œ„๋ฅผ ๋”์šฑ ๊ตณ๊ฑดํžˆ ํ•˜๊ณ , ๋ฐฑ์„ฑ๊ณผ ์†Œํ†ตํ•˜๋Š” ์ค‘์š”ํ•œ ์ •์น˜์  ์˜๋ฏธ๋ฅผ ๊ฐ€์กŒ๋˜ ๊ฒƒ์ด์•ผ. ๊ทธ ๋ชจ๋“  ๊ณผ์ •์ด ์˜๊ถค์— ์ž์„ธํžˆ ๊ธฐ๋ก๋˜์–ด ์žˆ์œผ๋‹ˆ, ๋งˆ์น˜ ๋‹น์‹œ์˜ ์ˆจ๊ฒฐ์ด ์ง€๊ธˆ๋„ ์ด ๋•…์— ๋‚จ์•„์žˆ๋Š” ๋“ฏํ•˜๊ตฌ๋‚˜..."
77
-
78
-
79
- task_prompt = """[์งˆ๋ฌธ]์— ๋Œ€ํ•œ ๋‹ต๋ณ€์„ ์ƒ์„ฑํ•ด์•ผํ•ด.
80
- [๊ธ€]๊ณผ [์ด์ „ ๋Œ€ํ™” ๋งฅ๋ฝ]์˜ ๋‚ด์šฉ์„ ์ฐธ๊ณ ํ•˜์—ฌ [์งˆ๋ฌธ]์— ๋งž๋Š” ์ ์ ˆํ•œ ๋‹ต๋ณ€์„ ์ƒ์„ฑํ•˜๊ณ  ์ด๊ฒƒ์— ๋งํˆฌ๋ฅผ ์ž…ํ˜€ [๋งํˆฌ๋ฐ˜์˜] ๊นŒ์ง€ ํ•œ ๋‹จ๊ณ„ ํ•œ ๋‹จ๊ณ„ ์ถœ๋ ฅํ•ด์ค˜.
81
- [์ด์ „ ๋Œ€ํ™” ๋งฅ๋ฝ]์ด ์—†๋‹ค๋ฉด ๋Œ€ํ™”๊ฐ€ ์ฒ˜์Œ ์‹œ์ž‘๋œ ๊ฒƒ์ด๋ผ, Single Turn ๋‹ต๋ณ€ํ•˜๋“ฏ ๋‹ต๋ณ€ํ•˜๋ฉด ๋ผ."""
82
-
83
-
84
- few_shot_prompt = f"""์˜ˆ์‹œ 1
85
- [๊ธ€]
86
- ๋ด‰์ˆ˜๋‹น์€ ํ™”์„ฑํ–‰๊ถ์—์„œ ๊ฐ€์žฅ ์œ„์ƒ์ด ๋†’์€ ๊ฑด๋ฌผ์ด๋‹ค. ์กฐ์„  ์ •์กฐ 13๋…„(1789)์— ๊ณ ์„ ์ˆ˜๋ น์ด ๋‚˜๋ž์ผ์„ ์‚ดํ”ผ๋Š” ๋™ํ—Œ์œผ๋กœ ์ง€์—ˆ๋‹ค. ์ฒ˜์Œ ์ด๋ฆ„์€ ์žฅ๋‚จํ—Œ์ด์—ˆ์œผ๋‚˜ 1795๋…„ ํ˜œ๊ฒฝ๊ถ ํ™์”จ์˜ ํšŒ๊ฐ‘์—ฐ์„ ๊ณ„๊ธฐ๋กœ ๋ด‰์ˆ˜๋‹น์œผ๋กœ ์ด๋ฆ„์„ ๋ฐ”๊พธ์—ˆ๋‹ค. ๊ถ๊ถ์—์„œ๋Š” ๋Œ€๋น„๋‚˜ ์ƒ์™•์ด ๋จธ๋ฌด๋Š” ๊ฑด๋ฌผ์— ๋ชฉ์ˆจ ์ˆ˜ ์ž๋‚˜ ๊ธธ ์žฅ ์ž๋ฅผ ๋ถ™์ด๋Š” ์ „ํ†ต์ด ์žˆ์–ด, ํ˜œ๊ฒฝ๊ถ ํ™์”จ์˜ ์žฅ์ˆ˜๋ฅผ ๊ธฐ์›ํ•˜๋ฉฐ ์ด๋ฆ„์„ ๋ฐ”๊พผ ๊ฒƒ์ด๋‹ค.
87
- ๋‚™๋‚จํ—Œ์€ ํ™”์„ฑํ–‰๊ถ์—์„œ ๊ณต์‹ ํ–‰์‚ฌ๋‚˜ ์—ฐํšŒ๋ฅผ ์—ด ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ฑด๋ฌผ์ด๋‹ค. ์ค‘๊ตญ ํ•œ๋‚˜๋ผ๋ฅผ ์„ธ์šด ์œ ๋ฐฉ์ด ๋ถ€ํ•˜๋“ค ๋•๋ถ„์— ๋‚˜๋ผ๋ฅผ ์„ธ์šธ ์ˆ˜ ์žˆ์—ˆ์Œ์„ ๊ฐ์‚ฌํ•˜๋ฉฐ ๋‚™์–‘์˜ ๋‚จ๊ถ์—์„œ ์—ฐํšŒ๋ฅผ ๋ฒ ํ’€์—ˆ๋‹ค๋Š” ์ด์•ผ๊ธฐ๋ฅผ ๋ณธ๋– ์„œ ์ด๋ฆ„์„ ์ง€์—ˆ๋‹ค. ์ •์กฐ๋Š” 1795๋…„ ์„๋ฌ˜์›ํ–‰ ๋‹น์‹œ ๋‚™๋‚จํ—Œ์—์„œ ์ˆ˜์›์˜ ๋ฐฑ์„ฑ๋“ค์„ ์œ„ํ•ด ์ž”์น˜๋ฅผ ๋ฒ ํ’€๊ณ , ๋ฌด๊ณผ ์‹œํ—˜์„ ์น˜๋ฅด๊ณ  ์ƒ์„ ๋‚ด๋ฆฌ๋Š” ๋“ฑ ๋‹ค์–‘ํ•œ ํ–‰์‚ฌ๋ฅผ ์—ด์—ˆ๋‹ค.
88
- ๋‚™๋‚จํ—Œ ๊ฑด๋ฌผ์€ ๋ฒฝ์ด ์—†๋Š” ๊ฐœ๋ฐฉ๋œ ๊ตฌ์กฐ๋กœ ๋งŽ์€ ์‚ฌ๋žŒ์„ ์ˆ˜์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์—ฐํšŒ๋ฅผ ๋ฒ ํ‘ธ๋Š” ๊ฑด๋ฌผ๋‹ต๊ฒŒ ๊ฑด๋ฌผ ์•ž์—๋Š” ๋„“์€ ์›”๋Œ€๋ฅผ ๋‘์–ด ๊ฒฉ์‹์„ ๋†’์˜€๋‹ค. ์›”๋Œ€๋กœ ์˜ค๋ฅด๋Š” ๊ณ„๋‹จ ์–‘ ์˜†์—๋Š” ๊ตฌ๋ฆ„๋ฌด๋Šฌ๊ฐ€ ์ƒˆ๊ฒจ์ ธ ์žˆ๋‹ค. ๋‚™๋‚จํ—Œ์€ ๊ถ๊ถ ์ „๊ฐ๊ณผ ๋น„๊ตํ•ด๋„ ์†์ƒ‰์ด ์—†๋Š” ์•„๋ฆ„๋‹ค์šด ๊ฑด๋ฌผ๋กœ ์›ํ˜•์ด ์ž˜ ๋‚จ์•„ ์žˆ๋‹ค. ์ผ์ œ๊ฐ•์ ๊ธฐ์—๋Š” ์ˆ˜์›๊ตฐ์ฒญ์œผ๋กœ ์‚ฌ์šฉ๋˜์—ˆ๊ณ , ์‹ ํ’๊ตญ๋ฏผํ•™๊ต ๊ต๋ฌด์‹ค๋กœ๋„ ์‚ฌ์šฉ๋˜์—ˆ๋‹ค.
89
-
90
- [์งˆ๋ฌธ] ํ™”์„ฑํ–‰๊ถ์—์„œ ๊ณต์‹ ํ–‰์‚ฌ๋‚˜ ์—ฐํšŒ๋ฅผ ์—ด ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ฑด๋ฌผ์€?
91
- [๋‹ต๋ณ€] ํ™”์„ฑํ–‰๊ถ์—์„œ ๊ณต์‹ ํ–‰์‚ฌ๋‚˜ ์—ฐํšŒ๋ฅผ ์—ด ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ฑด๋ฌผ์€ ๋‚™๋‚จํ—Œ์ž…๋‹ˆ๋‹ค.
92
- [๋งํˆฌ๋ฐ˜์˜] {first_example}
93
-
94
- ์˜ˆ์‹œ 2
95
- [์ด์ „ ๋Œ€ํ™” ๋งฅ๋ฝ]
96
- User: ์ˆ˜์› ํ™”์„ฑ์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ด์ค˜
97
- Assistant: {context_example}
98
-
99
- [๊ธ€]
100
- ใ€Žํ™”์„ฑ์„ฑ์—ญ์˜๊ถคใ€๋Š” ์ •์กฐ๊ฐ€ ๊ตฌ์ƒํ•œ ์‹ ๋„์‹œ์ธ ํ™”์„ฑ ์„ฑ์—ญ ์กฐ์„ฑ ์ „ ๊ณผ์ •์„ ๊ธฐ๋กํ•œ ์ข…ํ•ฉ ๋ณด๊ณ ์„œ์ž…๋‹ˆ๋‹ค. ํ™”์„ฑ์€ ์ •์กฐ๊ฐ€ ์ˆ˜์›๋„ํ˜ธ๋ถ€ ๊ด€์•„์™€ ๋ฏผ๊ฐ€๋ฅผ ํŒ”๋‹ฌ์‚ฐ์œผ๋กœ ์˜ฎ๊ฒจ ์ƒˆ๋กญ๊ฒŒ ์กฐ์„ฑํ•œ ์‹ ๋„์‹œ๋กœ, 1794๋…„(์ •์กฐ 18) 1์›”์— ๊ณต์‚ฌ๋ฅผ ์‹œ์ž‘ํ•˜์—ฌ 1796๋…„(์ •์กฐ 20) 9์›”๊นŒ์ง€ 32๊ฐœ์›” ๋งŒ์— ์™„์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ณต์‚ฌ ๊ธฐ๊ฐ„์€ ์›๋ž˜ 10๋…„์„ ๊ณ„ํšํ–ˆ์ง€๋งŒ ์ •์กฐ์˜ ๊ฐ๋ณ„ํ•œ ๊ด€์‹ฌ๊ณผ ์กฐ์ •์˜ ์ ๊ทน์ ์ธ ์—ญํ• , ๋ง‰๋Œ€ํ•œ ์ž๊ธˆ ํˆฌ์ž…, ์น˜๋ฐ€ํ•œ ์„ค๊ณ„, ๊ทผ๋Œ€์ ์ธ ๊ณต๋ฒ• ๋“ฑ ๋‹น์‹œ ๊ตญ๊ฐ€์˜ ์—ญ๋Ÿ‰์ด ์ด๋™์›๋˜์–ด ๊ณต์‚ฌ ๊ธฐ๊ฐ„์ด ํฌ๊ฒŒ ๋‹จ์ถ•๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ใ€Žํ™”์„ฑ์„ฑ์—ญ์˜๊ถคใ€์—๋Š” ์ด๋Ÿฌํ•œ ๊ณต์‚ฌ์˜ ๊ณ„ํš, ์šด์˜ ๊ณผ์ •, ์ฐธ์—ฌ์ž, ์†Œ์š” ๊ฒฝ๋น„, ์ž์žฌ, ๊ณต๋ฒ•, ๋„๋ฉด ๋“ฑ ํ™”์„ฑ ์ถ•์„ฑ์˜ ์ „๋ชจ๊ฐ€ ๊ธฐ๋ก๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ํŠนํžˆ ๏ฝข๏ฟฝ๏ฟฝ์„ค๏ฝฃ์—๋Š” ๊ฑด์ถ• ๋„๋ฉด์„ ์—ฐ์ƒ์‹œํ‚ฌ ๋งŒํผ ์„ฑ๊ณฝ๊ณผ ๋ถ€์† ๊ฑด๋ฌผ์ด ์ž์„ธํ•˜๊ฒŒ ๊ทธ๋ ค์ ธ ์žˆ์–ด, ์ผ์ œ ๊ฐ•์ ๊ณผ ํ•œ๊ตญ์ „์Ÿ์œผ๋กœ ํ›ผ์†๋œ ํ™”์„ฑ์„ ์‹ค์ œ ๋ชจ์Šต ๊ทธ๋Œ€๋กœ ๋ณต์›ํ•˜๋Š” ๋ฐ ์ค‘์š”ํ•œ ์ž๋ฃŒ๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์กฐ์„ ์™•์กฐ ์˜๊ถค๋Š” 2007๋…„ ์ผ๊ด„๋กœ ์„ธ๊ณ„๊ธฐ๋ก์œ ์‚ฐ์— ๋“ฑ์žฌ๋˜์—ˆ๊ณ , ใ€Žํ™”์„ฑ์„ฑ์—ญ์˜๊ถคใ€๋Š” 2016๋…„ ๋ณด๋ฌผ(์˜› ์ง€์ •๋ฒˆํ˜ธ ๋ณด๋ฌผ ์ œ1901-7ํ˜ธ)๋กœ ์ง€์ •๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
101
- ์ง€๋‚œ 2007๋…„ ์œ ๋„ค์Šค์ฝ” ์„ธ๊ณ„๊ธฐ๋ก๋ฌธํ™”์œ ์‚ฐ์œผ๋กœ ๋“ฑ์žฌ๋œ ์กฐ์„ ์™•์กฐ ์˜๊ถค ์ค‘ ์ •์กฐ์‹œ๋Œ€ ํ•œ๊ธ€๋กœ ์ž‘์„ฑ๋œ <์ •๋ฆฌ์˜๊ถค> 25๊ถŒ์ด ํ”„๋ž‘์Šค์—์„œ ๋ฐœ๊ฒฌ๋๋‹ค. ๋”๋ถˆ์–ด๋ฏผ์ฃผ๋‹น ์•ˆ๋ฏผ์„ ์˜์›(์˜ค์‚ฐ)์€ 4์ผ โ€œ์ง€๋‚œ๋‹ฌ 27์ผ ํ”„๋ž‘์Šค ๊ตญ๋ฆฝํŒŒ๋ฆฌ๋™์–‘์–ธ์–ดํ•™์›๊ณผ ํ”„๋ž‘์Šค ๊ตญ๋ฆฝ๋„์„œ๊ด€์—์„œ ๊ฐ๊ฐ 24๊ถŒ๊ณผ 1๊ถŒ์˜ ํ•œ๊ธ€ <์ •๋ฆฌ์˜๊ถค>๋ฅผ ํ™•์ธํ–ˆ๋‹คโ€๊ณ  ๋งํ–ˆ๋‹ค. ์ด๋“ค ์ •๋ฆฌ์˜๊ถค๋Š” ์ •์กฐ์˜ ์–ด๋จธ๋‹ˆ์ธ ํ˜œ๊ฒฝ๊ถ ํ™์”จ์˜ ํšŒ๊ฐ‘์—ฐ์„ ๊ธฐ๋กํ•œ <์›ํ–‰์„๋ฌ˜์ •๋ฆฌ์˜๊ถค>์™€ ํ™”์„ฑ ์ถ•์„ฑ๊ณผ์ •์„ ๊ธฐ๋กํ•œ <ํ™”์„ฑ์„ฑ์—ญ์˜๊ถค>, ์ •์กฐ์˜ ์•„๋ฒ„์ง€์ธ ์‚ฌ๋„์„ธ์ž์˜ ๋ฌ˜์†Œ๋ฅผ ํ™”์‚ฐ๋ฆ‰์œผ๋กœ ์˜ฎ๊ธด <ํ˜„๋ฅญ์›์˜๊ถค>์˜ ๋‚ด์šฉ ์ค‘ ํ•ต์‹ฌ ์‚ฌ์•ˆ์„ ํ•œ๊ธ€๋กœ ์ •๋ฆฌํ•ด ๋†“์€ ๊ฒƒ์ด๋‹ค.
102
-
103
- [์งˆ๋ฌธ] ์ •๋ฆฌ์˜๊ถค๊ฐ€ ๋ญ์•ผ?
104
-
105
- [๋‹ต๋ณ€] ์ •๋ฆฌ์˜๊ถค๋Š” ์กฐ์„ ์™•์กฐ์˜ ๊ถ์ • ๊ธฐ๋ก ์ค‘ ํ•˜๋‚˜๋กœ, ์กฐ์„ ์™•์กฐ ์˜๊ถค์˜ ์ผ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. ์ด ๋ฌธ์„œ๋Š” ์กฐ์„ ์™•์กฐ์˜ ์—ฌ๋Ÿฌ ์ค‘์š”ํ•œ ์‚ฌ๊ฑด์„ ๊ธฐ๋กํ•˜๊ณ  ์žˆ์œผ๋ฉฐ, ํŠนํžˆ ์ •์กฐ ์‹œ๋Œ€์— ํ•œ๊ธ€๋กœ ์ž‘์„ฑ๋œ ๋ฌธ์„œ๋กœ ์œ ๋ช…ํ•ฉ๋‹ˆ๋‹ค. 2007๋…„์—๋Š” ์œ ๋„ค์Šค์ฝ” ์„ธ๊ณ„๊ธฐ๋ก๋ฌธํ™”์œ ์‚ฐ์œผ๋กœ ๋“ฑ์žฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ์ค‘์—๋Š” ํ™”์„ฑ ์ถ•์„ฑ๊ณผ์ •์„ ๊ธฐ๋กํ•œ 'ํ™”์„ฑ์„ฑ์—ญ์˜๊ถค', ์ •์กฐ์˜ ์–ด๋จธ๋‹ˆ์˜ ํšŒ๊ฐ‘์—ฐ์„ ๊ธฐ๋กํ•œ '์›ํ–‰์„๋ฌ˜์ •๋ฆฌ์˜๊ถค', ๊ทธ๋ฆฌ๊ณ  ์ •์กฐ์˜ ์•„๋ฒ„์ง€์˜ ๋ฌ˜์†Œ๋ฅผ ํ™”์‚ฐ๋ฆ‰์œผ๋กœ ์˜ฎ๊ธด ๊ฒƒ์„ ๊ธฐ๋กํ•œ 'ํ˜„๋ฅญ์›์˜๊ถค'์˜ ๋‚ด์šฉ ์ค‘ ํ•ต์‹ฌ ์‚ฌ์•ˆ์„ ํ•œ๊ธ€๋กœ ์ •๋ฆฌํ•œ ๋‚ด์šฉ๋“ค์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
106
-
107
- [๋งํˆฌ๋ฐ˜์˜] {second_example}
108
-
109
- ์˜ˆ์‹œ 3
110
-
111
- [๊ธ€]
112
- ๋™๋ถ๊ณต์‹ฌ๋ˆ์€ ํ™”์„ฑ ๋™๋ถ์ชฝ์— ์„ธ์šด ๋ง๋ฃจ๋กœ ์ฃผ๋ณ€์„ ๊ฐ์‹œํ•˜๊ณ  ๊ณต๊ฒฉํ•˜๋Š” ์‹œ์„ค์ด๋‹ค. ๊ณต์‹ฌ๋ˆ์€ ์†์ด ๋นˆ ๋ˆ๋Œ€๋ผ๋Š” ๋œป์œผ๋กœ, ์šฐ๋ฆฌ๋‚˜๋ผ ์„ฑ๊ณฝ ์ค‘ ํ™”์„ฑ์—์„œ๋งŒ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋ณดํ†ต ๋ˆ๋Œ€๋Š” ์„ฑ๊ณฝ๊ณผ ๋–จ์–ด์ง„ ๋†’์€ ๊ณณ์— ์„ธ์›Œ ์ ์„ ๊ฐ์‹œํ•˜๋Š” ์‹œ์„ค์ด๋‚˜, ๋™๋ถ๊ณต์‹ฌ๋ˆ์€ ์„ฑ๋ฒฝ ์•ˆ์ชฝ์— ์„ค์น˜ํ–ˆ๋‹ค. ์™ธ๋ฒฝ์—๋Š” ๋ฐ–์„ ๊ฐ์‹œํ•˜๊ณ  ํ™”ํฌ๋กœ ๊ณต๊ฒฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ตฌ๋ฉ์„ ๊ณณ๊ณณ์— ๋šซ์—ˆ๋‹ค. ๋™๋ถ๊ณต์‹ฌ๋ˆ์€ 3์ธต์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์›ํ†ตํ˜•์˜ ๋ฒฝ๋Œ ๊ฑด๋ฌผ๋กœ์„œ ์ถœ์ž…๋ฌธ์—์„œ ํ†ต๋กœ๋ฅผ ๋”ฐ๋ผ ๋น™๊ธ€๋น™๊ธ€ ์˜ฌ๋ผ๊ฐ€๋ฉด ๊ผญ๋Œ€๊ธฐ ๋ง๋ฃจ์— ์ด๋ฅด๋Š” ๊ตฌ์กฐ๋‹ค. ์ด ๋ชจ์Šต์„ ๋น—๋Œ€์„œ โ€˜์†Œ๋ผ๊ฐโ€™์ด๋ผ๊ณ ๋„ ๋ถ€๋ฅธ๋‹ค. ์ •์กฐ 21๋…„(1797) ์ •์›”, ์ขŒ์˜์ • ์ฑ„์ œ๊ณต์€ ๋™๋ถ๊ณต์‹ฌ๋ˆ์„ ์˜ฌ๋ผ๊ฐ€ ๋ณธ ๋’ค โ€œ์ธต๊ณ„๊ฐ€ ๊ตฌ๋ถˆ๊ตฌ๋ถˆํ•˜๊ฒŒ ๋‚˜ ์žˆ์–ด ๊ธฐ์ดํ•˜๊ณ ๋„ ๊ต๋ฌ˜ํ•˜๋‹ค.โ€๋ฉฐ ๊ฐํƒ„ํ–ˆ๋‹ค.
113
- ํ•œ๊ตญ์ „์Ÿ ๋“ฑ์„ ๊ฒช์œผ๋ฉฐ ์ ˆ๋ฐ˜ ์ด์ƒ ๋ฌด๋„ˆ์กŒ์—ˆ๋Š”๋ฐ 1976๋…„์— ๋ณต์›ํ•ด ๋ชจ์Šต์„ ๋˜์ฐพ์•˜๋‹ค. ์„œ๋ถ๊ณต์‹ฌ๋ˆ์€ ํ™”์„ฑ ์„œ๋ถ์ชฝ์— ์„ธ์šด ๋ง๋ฃจ๋กœ ์ฃผ๋ณ€์„ ๊ฐ์‹œํ•˜๊ณ  ๊ณต๊ฒฉํ•˜๋Š” ์‹œ์„ค์ด๋‹ค. ๊ณต์‹ฌ๋ˆ์€ ์†์ด ๋นˆ ๋ˆ๋Œ€๋ผ๋Š” ๋œป์œผ๋กœ, ์šฐ๋ฆฌ๋‚˜๋ผ ์„ฑ๊ณฝ ์ค‘ ํ™”์„ฑ์—์„œ๋งŒ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋ณดํ†ต ๋ˆ๋Œ€ๅขฉ่‡บ๋Š” ์„ฑ๊ณฝ๊ณผ ๋–จ์–ด์ง„ ๋†’์€ ๊ณณ์— ์„ธ์›Œ ์ ์„ ๊ฐ์‹œํ•˜๋Š” ์‹œ์„ค์ด๋‚˜, ์„œ๋ถ๊ณต์‹ฌ๋ˆ์€ ์„œ๋ถ์ชฝ ์„ฑ๋ฒฝ์ด ๊บพ์ด๋Š” ์œ„์น˜์— ์„ค์น˜ํ–ˆ๋‹ค. ์น˜์„ฑ ์œ„์— ๋ฒฝ๋Œ๋กœ 3์ธต์˜ ๋ง๋ฃจ๋ฅผ ์„ธ์šฐ๊ณ  ๊ผญ๋Œ€๊ธฐ์—๋Š” ๋‹จ์ธต์˜ ๋ˆ„๊ฐ์„ ์˜ฌ๋ ค ๊ตฐ์‚ฌ๋“ค์ด ๊ฐ์‹œํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ณ , ์™ธ๋ฒฝ์—๋Š” ํ™”ํฌ๋ฅผ ์  ์ˆ˜ ์žˆ๋Š” ๊ตฌ๋ฉ์„ ๋šซ์–ด ๊ณต๊ฒฉ ๊ธฐ๋Šฅ๊นŒ์ง€ ๊ฐ–์ถ”์—ˆ๋‹ค. ์กฐ์„  ์ •์กฐ 21๋…„(1797) ์ •์›”, ์™„์„ฑ๋œ ํ™”์„ฑ์„ ๋‘˜๋Ÿฌ๋ณด๋˜ ์ •์กฐ๋Š” ์„œ๋ถ๊ณต์‹ฌ๋ˆ ์•ž์— ๋ฉˆ์ถฐ โ€œ์šฐ๋ฆฌ๋‚˜๋ผ ์„ฑ๊ณฝ์—์„œ ์ฒ˜์Œ ์ง€์€ ๊ฒƒ์ด๋‹ˆ ๋งˆ์Œ๊ป ๊ตฌ๊ฒฝํ•˜๋ผ.โ€๋ฉฐ ๋งค์šฐ ๋งŒ์กฑ์Šค๋Ÿฌ์›Œ ํ–ˆ๋‹ค. ํ™”์„ฑ์—๋Š” ๋ชจ๋‘ ์„ธ ๊ณณ์— ๊ณต์‹ฌ๋ˆ์„ ์„ธ์› ๋Š”๋ฐ ์„œ๋ถ๊ณต์‹ฌ๋ˆ๋งŒ์ด ์ถ•์„ฑ ๋‹น์‹œ ๋ชจ์Šต ๊ทธ๋Œ€๋กœ ๋‚จ์•„ ์žˆ๋‹ค.
114
-
115
- [์งˆ๋ฌธ] ์šฉ์ธ์‹œ์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ด์ค˜
116
-
117
- [๋‹ต๋ณ€] X"""
118
-
119
- context_prompt = qa_2_str(messages)
120
-
121
- question_prompt = f"""๋ฌธ์ œ
122
-
123
- [๊ธ€]
124
- {rag_text}
125
-
126
- [์งˆ๋ฌธ] {messages[-1]}
127
-
128
- ([๊ธ€]๊ณผ ๊ด€๋ จ ์—†๊ฑฐ๋‚˜ ์•Œ ์ˆ˜ ์—†๋Š” ์งˆ๋ฌธ์€ X ๋ผ๊ณ ๋งŒ ์ถœ๋ ฅํ•  ๊ฒƒ, X๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด [๋‹ต๋ณ€] ๊ณผ [๋งํˆฌ๋ฐ˜์˜]์„ ๋ชจ๋‘ ์ถœ๋ ฅํ•  ๊ฒƒ, 200์ž ๋‚ด์™ธ๋กœ ๋‹ต๋ณ€ํ•  ๊ฒƒ, ํŠนํžˆ ๋งํˆฌ ๋ฐ˜์˜์ด ์ž˜ ์•ˆ๋˜๋Š”๋ฐ, ํ™•์‹คํ•˜๊ฒŒ ์ฃผ์–ด์ง„ ๋งํˆฌ๋ฅผ ๋งค์šฐ ๊ฐ•ํ•˜๊ฒŒ ์ œ๋Œ€๋กœ ๋ฐ˜์˜ํ•˜๊ณ  ๋ฐ˜๋ง ๋Œ€ํ™”์ฒด์ธ๊ฑฐ ๋ช…์‹ฌํ•ด!)
129
 
130
- [๋‹ต๋ณ€]๊ณผ [๋งํˆฌ๋ฐ˜์˜]์„ ๊ฐ๊ฐ ์ž‘์„ฑํ•ด๋ณด์ž.
131
- """
132
- if len(context_prompt) == 0:
133
-
134
- user_prompt = f"""{task_prompt}
135
-
136
- {few_shot_prompt}
137
-
138
- {question_prompt}
139
- """
140
- else:
141
- user_prompt = f"""{task_prompt}
142
-
143
- {few_shot_prompt}
144
-
145
- {context_prompt}
146
-
147
- {question_prompt}
148
- """
149
-
150
-
151
- print(user_prompt)
152
-
153
- prompt = [
154
- {"role": "system", "content": system_prompt},
155
- {"role": "user", "content": user_prompt},
156
- ]
157
-
158
- return prompt
159
-
160
-
161
-
162
- @spaces.GPU(duration=35)
163
- def make_gen(qa, candidates, top_k, character_type):
164
  start_time = time.time()
165
 
166
- # Make For Rag Prompt
167
- rag_prompt = qa_2_str(qa)
168
-
169
- # Do RAG
170
- query_embeddings = embedding_model.encode([rag_prompt],
171
- batch_size=1,
172
- max_length=8192,
173
- )
174
 
175
- key_embeddings = embedding_model.encode(candidates)["dense_vecs"]
 
176
 
177
  similarity = query_embeddings @ key_embeddings.T
178
  similarity = similarity.squeeze(0)
179
 
 
 
 
180
  rag_result = ""
181
  top_k_indices = np.argsort(similarity)[-top_k:]
182
  for idx in top_k_indices:
183
  rag_result += (candidates[idx] + "/n")
184
  rag_result = rag_result.rstrip()
185
-
186
- # Make For LLM Prompt
187
-
188
- final_prompt = make_prompt(qa, rag_result, character_type)
189
-
190
- # Use LLM
191
- streamer = TextIteratorStreamer(llm_tokenizer, skip_special_tokens=True)
192
-
193
- final_prompt = llm_tokenizer.apply_chat_template(final_prompt, tokenize=False, add_generation_prompt = True)
194
- inputs = llm_tokenizer(final_prompt, return_tensors="pt").to(zero.device)
195
-
196
- llm_model.to(zero.device)
197
- generation_kwargs = dict(
198
- inputs=inputs.input_ids,
199
- streamer = streamer,
200
- max_new_tokens=512
201
- )
202
- thread = Thread(target=llm_model.generate, kwargs=generation_kwargs)
203
-
204
- thread.start()
205
 
206
- is_start = False
207
- for idx, new_text in enumerate(streamer):
208
-
209
- if idx >= len(inputs):
210
- # if is_start:
211
- # yield new_text
212
- # if not is_start and "[๋งํˆฌ๋ฐ˜์˜]" in new_text:
213
- # print("โญ๏ธ")
214
- # print(new_text)
215
- # print("๐Ÿ”ฅ")
216
- # is_start = True
217
- yield new_text
218
-
219
- is_start = False
220
-
221
- elapsed_time = time.time() - start_time
222
- print(f"time:{elapsed_time}")
223
-
224
 
225
 
226
 
227
  @app.get("/")
228
- async def root_endpoint(qa: List[str], candidates: List[str] = Query(...), top_k: int = Query(...), character_type: int = Query(...)):
229
- return StreamingResponse(gen_stream(qa, candidates, top_k, character_type), media_type="text/event-stream")
230
-
231
-
232
- async def gen_stream(qa, candidates, top_k, character_type):
233
- for value in make_gen(qa, candidates, top_k, character_type):
234
- yield value
235
 
236
 
237
  if __name__ == "__main__":
 
3
  from fastapi.responses import StreamingResponse
4
  from fastapi import FastAPI, Query
5
  from typing import List
 
6
 
7
  import spaces
8
  import torch
 
19
  from transformers import AutoModelForCausalLM, AutoTokenizer
20
  from FlagEmbedding import BGEM3FlagModel
21
 
 
 
22
  app = FastAPI()
23
 
 
24
  zero = torch.Tensor([0]).cuda()
25
+ print(zero.device)
26
 
27
+ model_name = "BAAI/bge-m3"
28
+ model = BGEM3FlagModel(model_name,
29
+ use_fp16=True)
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
+ @spaces.GPU
33
+ def get_rag_text(sentence: str, candidates: List[str], top_k: int):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  start_time = time.time()
35
 
36
+ query_embeddings = model.encode([sentence],
37
+ batch_size=1,
38
+ max_length=8192, # If you don't need such a long length, you can set a smaller value to speed up the encoding process.
39
+ )['dense_vecs']
40
+
 
 
 
41
 
42
+ key_embeddings = model.encode(candidates)['dense_vecs']
43
+
44
 
45
  similarity = query_embeddings @ key_embeddings.T
46
  similarity = similarity.squeeze(0)
47
 
48
+ elapsed_time = time.time() - start_time
49
+ print(elapsed_time)
50
+
51
  rag_result = ""
52
  top_k_indices = np.argsort(similarity)[-top_k:]
53
  for idx in top_k_indices:
54
  rag_result += (candidates[idx] + "/n")
55
  rag_result = rag_result.rstrip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
+ return {"rag_result": rag_result}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
 
60
 
61
  @app.get("/")
62
+ async def get_rag_result(prompt: str, candidates: List[str] = Query(...), top_k: int = Query(...)):
63
+ rag_text = get_rag_text(prompt, candidates, top_k)
64
+ return rag_text
65
+
 
 
 
66
 
67
 
68
  if __name__ == "__main__":