2. μŠ€λ§ˆνŠΈν•œ ν”„λ‘¬ν”„νŠΈ μ—”μ§€λ‹ˆμ–΄λ§

🎯 이 μ±•ν„°μ—μ„œ 배울 것

  • FewShotPromptTemplate을 μ‚¬μš©ν•˜μ—¬ λͺ¨λΈμ—κ²Œ μ˜ˆμ‹œλ₯Ό μ œκ³΅ν•˜κ³  μ„±λŠ₯을 λ†’μ΄λŠ” 방법
  • ExampleSelectorλ₯Ό μ‚¬μš©ν•˜μ—¬ λ™μ μœΌλ‘œ μ˜ˆμ‹œλ₯Ό μ„ νƒν•˜λŠ” 방법
  • μ—¬λŸ¬ ν”„λ‘¬ν”„νŠΈ ν…œν”Œλ¦Ώμ„ PipelinePromptTemplate으둜 κ²°ν•©ν•˜λŠ” 방법
  • LLM 호좜 κ²°κ³Όλ₯Ό μΊμ‹±ν•˜μ—¬ λΉ„μš©κ³Ό μ‹œκ°„μ„ μ ˆμ•½ν•˜λŠ” 방법
  • ν”„λ‘¬ν”„νŠΈμ™€ λͺ¨λΈ 섀정을 파일둜 μ €μž₯ν•˜κ³  λΆˆλŸ¬μ˜€λŠ” 직렬화(Serialization) 방법

FewShotPromptTemplate

🎯 이번 λ‹¨κ³„μ—μ„œ 배울 것

  • FewShotPromptTemplate의 κ°œλ…κ³Ό ν•„μš”μ„± μ΄ν•΄ν•˜κΈ°
  • λͺ¨λΈμ—κ²Œ λͺ‡ κ°€μ§€ μ˜ˆμ‹œ(few-shot)λ₯Ό μ œκ³΅ν•˜μ—¬ μ›ν•˜λŠ” 좜λ ₯ ν˜•μ‹μ„ ν•™μŠ΅μ‹œν‚€λŠ” 방법

πŸ“ 1단계: Few-Shot ν”„λ‘¬ν”„νŒ… μ μš©ν•˜κΈ°

전체 μ½”λ“œ (notebook.ipynb):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from langchain.chat_models import ChatOpenAI
from langchain.prompts.few_shot import FewShotPromptTemplate
from langchain.callbacks import StreamingStdOutCallbackHandler
from langchain.prompts.prompt import PromptTemplate

chat = ChatOpenAI(
temperature=0.1,
streaming=True,
callbacks=[StreamingStdOutCallbackHandler()],
)

examples = [
{
"question": "What do you know about France?",
"answer": """
Here is what I know:
Capital: Paris
Language: French
Food: Wine and Cheese
Currency: Euro
""",
},
# ... (λ‹€λ₯Έ μ˜ˆμ‹œλ“€)
]

example_prompt = PromptTemplate.from_template("Human: {question}\nAI:{answer}")

prompt = FewShotPromptTemplate(
example_prompt=example_prompt,
examples=examples,
suffix="Human: What do you know about {country}?",
input_variables=["country"],
)

chain = prompt | chat
chain.invoke({"country": "Turkey"})

πŸ” μ½”λ“œ 상세 μ„€λͺ…

1. Few-Shot ν”„λ‘¬ν”„νŒ…μ΄λž€?
λͺ¨λΈμ—κ²Œ λ‹¨μˆœνžˆ 질문만 λ˜μ§€λŠ” 것(Zero-Shot)이 μ•„λ‹ˆλΌ, λͺ‡ κ°€μ§€ 질문과 λ‹΅λ³€μ˜ μ˜ˆμ‹œλ₯Ό ν•¨κ»˜ μ œκ³΅ν•˜μ—¬ λͺ¨λΈμ΄ 더 μ •ν™•ν•˜κ³  μΌκ΄€λœ ν˜•μ‹μœΌλ‘œ λ‹΅λ³€ν•˜λ„λ‘ μœ λ„ν•˜λŠ” κΈ°λ²•μž…λ‹ˆλ‹€.

  • μ™œ μ‚¬μš©ν•˜λŠ”κ°€?: λͺ¨λΈμ΄ λ³΅μž‘ν•œ μ§€μ‹œλ₯Ό 더 잘 λ”°λ₯΄κ²Œ ν•˜κ±°λ‚˜, νŠΉμ • 좜λ ₯ ν˜•μ‹μ„ μœ μ§€ν•΄μ•Ό ν•  λ•Œ 맀우 μœ μš©ν•©λ‹ˆλ‹€.

2. FewShotPromptTemplate
이 ν…œν”Œλ¦Ώμ€ μ—¬λŸ¬ 개의 μ˜ˆμ‹œ(examples)와 μ˜ˆμ‹œμ˜ ν˜•μ‹μ„ μ •μ˜ν•˜λŠ” example_prompt, 그리고 μ‹€μ œ μ‚¬μš©μžμ˜ 질문 λΆ€λΆ„(suffix)을 μ‘°ν•©ν•˜μ—¬ μ΅œμ’… ν”„λ‘¬ν”„νŠΈλ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.

λ™μž‘ 흐름:

1
2
3
4
5
6
7
8
9
10
11
12
1. `examples` λ¦¬μŠ€νŠΈμ— μžˆλŠ” 각 μ˜ˆμ‹œκ°€ `example_prompt`에 따라 ν¬λ§·νŒ…λ¨
(예: "Human: What do you know about France?\nAI:Here is what I know...")
|
V
2. ν¬λ§·νŒ…λœ λͺ¨λ“  μ˜ˆμ‹œλ“€μ΄ ν•˜λ‚˜λ‘œ 합쳐짐
|
V
3. λ§ˆμ§€λ§‰μ— `suffix`κ°€ 좔가됨
(예: "Human: What do you know about Turkey?")
|
V
4. μ™„μ„±λœ ν”„λ‘¬ν”„νŠΈκ°€ `chat` λͺ¨λΈλ‘œ 전달됨

βœ… 체크리슀트

  • FewShotPromptTemplate을 import ν–ˆλ‚˜μš”?
  • μ—¬λŸ¬ 개의 μ˜ˆμ‹œλ₯Ό 담은 λ”•μ…”λ„ˆλ¦¬ 리슀트λ₯Ό λ§Œλ“€μ—ˆλ‚˜μš”?
  • PromptTemplate으둜 μ˜ˆμ‹œμ˜ ν˜•μ‹μ„ μ •μ˜ν–ˆλ‚˜μš”?
  • FewShotPromptTemplate에 examples, example_prompt, suffixλ₯Ό μ „λ‹¬ν•˜μ—¬ μ΅œμ’… ν”„λ‘¬ν”„νŠΈλ₯Ό μƒμ„±ν–ˆλ‚˜μš”?

FewShotChatMessagePromptTemplate

🎯 이번 λ‹¨κ³„μ—μ„œ 배울 것

  • ChatOpenAI와 같은 μ±— λͺ¨λΈμ— νŠΉν™”λœ FewShotChatMessagePromptTemplate μ‚¬μš©λ²•
  • ChatPromptTemplate을 ν™œμš©ν•˜μ—¬ λŒ€ν™” ν˜•μ‹μ˜ μ˜ˆμ‹œλ₯Ό κ΅¬μ„±ν•˜λŠ” 방법

πŸ“ 1단계: λŒ€ν™”ν˜• Few-Shot ν”„λ‘¬ν”„νŒ…

전체 μ½”λ“œ (notebook.ipynb):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from langchain.prompts.few_shot import FewShotChatMessagePromptTemplate
from langchain.prompts import ChatPromptTemplate

# ... (chat, examples 섀정은 이전과 μœ μ‚¬)

example_prompt = ChatPromptTemplate.from_messages(
[
("human", "What do you know about {country}?"),
("ai", "{answer}"),
]
)

example_prompt = FewShotChatMessagePromptTemplate(
example_prompt=example_prompt,
examples=examples,
)

final_prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a geography expert, you give short answers."),
example_prompt,
("human", "What do you know about {country}?"),
]
)

chain = final_prompt | chat
chain.invoke({"country": "Thailand"})

πŸ” μ½”λ“œ 상세 μ„€λͺ…

1. FewShotChatMessagePromptTemplate
이전 λ‹¨κ³„μ˜ FewShotPromptTemplate이 전체 ν”„λ‘¬ν”„νŠΈλ₯Ό ν•˜λ‚˜μ˜ 큰 λ¬Έμžμ—΄λ‘œ λ§Œλ“œλŠ” 반면, 이 ν…œν”Œλ¦Ώμ€ SystemMessage, HumanMessage, AIMessage λ“± λ©”μ‹œμ§€ 객체의 리슀트λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€. μ΄λŠ” μ±— λͺ¨λΈμ— 더 μ ν•©ν•œ λ°©μ‹μž…λ‹ˆλ‹€.

  • ChatPromptTemplate.from_messages: ("human", "..."), ("ai", "...") 와 같은 νŠœν”Œμ„ μ‚¬μš©ν•˜μ—¬ λŒ€ν™” ν˜•μ‹μ˜ μ˜ˆμ‹œ ν…œν”Œλ¦Ώμ„ μ‰½κ²Œ λ§Œλ“€ 수 μžˆμŠ΅λ‹ˆλ‹€.

λ™μž‘ 흐름:

1
2
3
4
5
6
7
8
9
10
11
12
13
1. `final_prompt`κ°€ `invoke`의 μž…λ ₯을 λ°›μŒ
|
V
2. `SystemMessage`κ°€ 생성됨
|
V
3. `FewShotChatMessagePromptTemplate`이 `examples`λ₯Ό `ChatPromptTemplate` ν˜•μ‹μ— 맞좰 `HumanMessage`와 `AIMessage`의 쌍으둜 된 리슀트λ₯Ό 생성
|
V
4. λ§ˆμ§€λ§‰ `HumanMessage`κ°€ 생성됨
|
V
5. 이 λͺ¨λ“  λ©”μ‹œμ§€ 객체듀이 λ‹΄κΈ΄ λ¦¬μŠ€νŠΈκ°€ `chat` λͺ¨λΈλ‘œ 전달됨

βœ… 체크리슀트

  • FewShotChatMessagePromptTemplate을 import ν–ˆλ‚˜μš”?
  • ChatPromptTemplate.from_messagesλ₯Ό μ‚¬μš©ν•˜μ—¬ λŒ€ν™”ν˜• μ˜ˆμ‹œ ν…œν”Œλ¦Ώμ„ λ§Œλ“€μ—ˆλ‚˜μš”?
  • μ΅œμ’… ChatPromptTemplate에 μ‹œμŠ€ν…œ λ©”μ‹œμ§€, few-shot ν”„λ‘¬ν”„νŠΈ, μ‚¬μš©μž μ§ˆλ¬Έμ„ λͺ¨λ‘ ν¬ν•¨μ‹œμΌ°λ‚˜μš”?

Example Selectors

🎯 이번 λ‹¨κ³„μ—μ„œ 배울 것

  • BaseExampleSelectorλ₯Ό 상속받아 μ»€μŠ€ν…€ μ˜ˆμ‹œ 선택기λ₯Ό λ§Œλ“œλŠ” 방법
  • ν”„λ‘¬ν”„νŠΈ 길이λ₯Ό κ³ λ €ν•˜μ—¬ λ™μ μœΌλ‘œ μ˜ˆμ‹œλ₯Ό μ„ νƒν•˜λŠ” LengthBasedExampleSelector의 ν•„μš”μ„± μ΄ν•΄ν•˜κΈ°

πŸ“ 1단계: 랜덀 μ˜ˆμ‹œ 선택기 λ§Œλ“€κΈ°

전체 μ½”λ“œ (notebook.ipynb):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from langchain.prompts.example_selector.base import BaseExampleSelector

class RandomExampleSelector(BaseExampleSelector):
def __init__(self, examples):
self.examples = examples

def add_example(self, example):
self.examples.append(example)

def select_examples(self, input_variables):
from random import choice
return [choice(self.examples)]

# ... (prompt, chat 섀정은 이전과 μœ μ‚¬)

example_selector = RandomExampleSelector(examples=examples)

prompt = FewShotPromptTemplate(
example_prompt=example_prompt,
example_selector=example_selector, # 'examples' λŒ€μ‹  'example_selector' μ‚¬μš©
suffix="Human: What do you know about {country}?",
input_variables=["country"],
)

πŸ” μ½”λ“œ 상세 μ„€λͺ…

1. ExampleSelectorλž€?
λͺ¨λΈμ—κ²Œ μ œκ³΅ν•  μ˜ˆμ‹œλ₯Ό λ™μ μœΌλ‘œ μ„ νƒν•˜λŠ” λ‘œμ§μ„ 담은 ν΄λž˜μŠ€μž…λ‹ˆλ‹€. λͺ¨λ“  μ˜ˆμ‹œλ₯Ό λ‹€ λ„£κΈ°μ—λŠ” ν”„λ‘¬ν”„νŠΈκ°€ λ„ˆλ¬΄ κΈΈμ–΄μ§ˆ λ•Œ (토큰 μ œν•œ 초과) 특히 μœ μš©ν•©λ‹ˆλ‹€.

  • BaseExampleSelector: μ»€μŠ€ν…€ 선택기λ₯Ό λ§Œλ“€λ €λ©΄ 이 클래슀λ₯Ό μƒμ†ν•˜κ³  select_examples λ©”μ†Œλ“œλ₯Ό κ΅¬ν˜„ν•΄μ•Ό ν•©λ‹ˆλ‹€.
  • RandomExampleSelector: 이 μ˜ˆμ œμ—μ„œλŠ” 전체 μ˜ˆμ‹œ 쀑 ν•˜λ‚˜λ₯Ό λ¬΄μž‘μœ„λ‘œ μ„ νƒν•˜λŠ” κ°„λ‹¨ν•œ 선택기λ₯Ό λ§Œλ“€μ—ˆμŠ΅λ‹ˆλ‹€.
  • LengthBasedExampleSelector: LangChain에 λ‚΄μž₯된 μ„ νƒκΈ°λ‘œ, ν”„λ‘¬ν”„νŠΈμ˜ 총 길이λ₯Ό κ³„μ‚°ν•˜μ—¬ 토큰 μ œν•œμ„ λ„˜μ§€ μ•Šλ„λ‘ μ˜ˆμ‹œ 개수λ₯Ό μ‘°μ ˆν•΄μ£ΌλŠ” 맀우 μ‹€μš©μ μΈ λ„κ΅¬μž…λ‹ˆλ‹€.

2. FewShotPromptTemplateκ³Ό ν•¨κ»˜ μ‚¬μš©ν•˜κΈ°
FewShotPromptTemplate을 생성할 λ•Œ, examples=[...] λŒ€μ‹  example_selector=MySelector(...)λ₯Ό μ „λ‹¬ν•˜λ©΄, ν”„λ‘¬ν”„νŠΈλ₯Ό 포맷할 λ•Œλ§ˆλ‹€ μ„ νƒκΈ°μ˜ select_examples λ©”μ†Œλ“œκ°€ ν˜ΈμΆœλ˜μ–΄ λ™μ μœΌλ‘œ μ˜ˆμ‹œκ°€ μ„ νƒλ©λ‹ˆλ‹€.

βœ… 체크리슀트

  • BaseExampleSelectorλ₯Ό μƒμ†ν•˜μ—¬ λ‚˜λ§Œμ˜ 선택기λ₯Ό λ§Œλ“€μ—ˆλ‚˜μš”?
  • select_examples λ©”μ†Œλ“œλ₯Ό κ΅¬ν˜„ν•˜μ—¬ μ˜ˆμ‹œλ₯Ό μ„ νƒν•˜λŠ” λ‘œμ§μ„ μž‘μ„±ν–ˆλ‚˜μš”?
  • FewShotPromptTemplate에 example_selectorλ₯Ό μ „λ‹¬ν•˜μ—¬ μ‚¬μš©ν–ˆλ‚˜μš”?

Serialization and Composition (PipelinePromptTemplate)

🎯 이번 λ‹¨κ³„μ—μ„œ 배울 것

  • μ—¬λŸ¬ 개의 ν”„λ‘¬ν”„νŠΈ ν…œν”Œλ¦Ώμ„ ν•˜λ‚˜μ˜ νŒŒμ΄ν”„λΌμΈμœΌλ‘œ κ²°ν•©ν•˜λŠ” PipelinePromptTemplate μ‚¬μš©λ²•
  • ν”„λ‘¬ν”„νŠΈλ₯Ό .jsonμ΄λ‚˜ .yaml 파일둜 μ €μž₯ν•˜κ³  λΆˆλŸ¬μ˜€λŠ” 방법 (직렬화)

πŸ“ 1단계: ν”„λ‘¬ν”„νŠΈ νŒŒμ΄ν”„λΌμΈ λ§Œλ“€κΈ°

전체 μ½”λ“œ (notebook.ipynb):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from langchain.prompts.pipeline import PipelinePromptTemplate
from langchain.prompts import PromptTemplate

# ... (chat μ„€μ •)

intro = PromptTemplate.from_template("You are impersonating a {character}")
example = PromptTemplate.from_template("Human: {example_question}\nYou: {example_answer}")
start = PromptTemplate.from_template("Human: {question}\nYou:")

final_prompt_str = "{intro}\n{example}\n{start}"
final_prompt = PromptTemplate.from_template(final_prompt_str)

prompts = [
("intro", intro),
("example", example),
("start", start),
]

full_prompt = PipelinePromptTemplate(
final_prompt=final_prompt,
pipeline_prompts=prompts,
)

chain = full_prompt | chat
chain.invoke({...})

πŸ” μ½”λ“œ 상세 μ„€λͺ…

1. PipelinePromptTemplate
μ—¬λŸ¬ 개의 μž‘μ€ ν”„λ‘¬ν”„νŠΈ ν…œν”Œλ¦Ώ 쑰각듀을 κ²°ν•©ν•˜μ—¬ ν•˜λ‚˜μ˜ μ΅œμ’… ν”„λ‘¬ν”„νŠΈλ₯Ό λ§Œλ“œλŠ” λ³΅μž‘ν•œ μž‘μ—…μ„ κ΄€λ¦¬ν•΄μ€λ‹ˆλ‹€. 각 쑰각은 λ…λ¦½μ μœΌλ‘œ ν¬λ§·νŒ…λœ ν›„, final_prompt에 μ˜ν•΄ μ΅œμ’…μ μœΌλ‘œ μ‘°ν•©λ©λ‹ˆλ‹€.

  • μ™œ μ‚¬μš©ν•˜λŠ”κ°€?: μ—­ν• , μ˜ˆμ‹œ, μ§€μ‹œμ‚¬ν•­ λ“± ν”„λ‘¬ν”„νŠΈμ˜ 각 뢀뢄을 λͺ¨λ“ˆν™”ν•˜μ—¬ μž¬μ‚¬μš©ν•˜κ³  κ΄€λ¦¬ν•˜κΈ° μš©μ΄ν•©λ‹ˆλ‹€.

2. 직렬화 (Serialization)
LangChain의 λ§Žμ€ ꡬ성 μš”μ†Œ(ν”„λ‘¬ν”„νŠΈ, 체인, λͺ¨λΈ λ“±)λŠ” .jsonμ΄λ‚˜ .yaml ν˜•μ‹μœΌλ‘œ μ‰½κ²Œ μ €μž₯ν•˜κ³  뢈러올 수 μžˆμŠ΅λ‹ˆλ‹€. μ΄λŠ” ν”„λ‘¬ν”„νŠΈλ₯Ό μ½”λ“œμ™€ λΆ„λ¦¬ν•˜μ—¬ κ΄€λ¦¬ν•˜κ±°λ‚˜, λ‹€λ₯Έ μ‚¬λžŒκ³Ό κ³΅μœ ν•  λ•Œ 맀우 μœ μš©ν•©λ‹ˆλ‹€.

μ˜ˆμ‹œ (prompt.yaml):

1
2
3
_type: "prompt"
template: "What is the capital of {country}"
input_variables: ["country"]
1
2
3
# 뢈러였기
from langchain.prompts import load_prompt
prompt = load_prompt("prompt.yaml")

βœ… 체크리슀트

  • μ—¬λŸ¬ 개의 PromptTemplate을 λ§Œλ“€μ—ˆλ‚˜μš”?
  • PipelinePromptTemplate을 μ‚¬μš©ν•˜μ—¬ μž‘μ€ ν…œν”Œλ¦Ώλ“€μ„ ν•˜λ‚˜μ˜ μ΅œμ’… ν”„λ‘¬ν”„νŠΈλ‘œ κ²°ν•©ν–ˆλ‚˜μš”?
  • ν”„λ‘¬ν”„νŠΈλ₯Ό .yaml λ˜λŠ” .json 파일둜 μ €μž₯ν•˜κ³  load_prompt둜 λΆˆλŸ¬μ™€ λ³΄μ•˜λ‚˜μš”?

Caching

🎯 이번 λ‹¨κ³„μ—μ„œ 배울 것

  • LLM 호좜 κ²°κ³Όλ₯Ό μΊμ‹±ν•˜μ—¬ API λΉ„μš©κ³Ό 응닡 μ‹œκ°„μ„ μ€„μ΄λŠ” 방법
  • InMemoryCache와 SQLiteCache의 차이점

πŸ“ 1단계: LLM μΊμ‹œ μ„€μ •ν•˜κΈ°

전체 μ½”λ“œ (notebook.ipynb):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from langchain.globals import set_llm_cache
from langchain.cache import InMemoryCache, SQLiteCache
from langchain.chat_models import ChatOpenAI

# μ „μ—­ μΊμ‹œ μ„€μ • (μ• ν”Œλ¦¬μΌ€μ΄μ…˜ μ‹œμž‘ μ‹œ ν•œ 번만 μ‹€ν–‰)
set_llm_cache(SQLiteCache("cache.db"))

chat = ChatOpenAI(temperature=0.1)

# 첫 번째 호좜: OpenAI APIλ₯Ό ν˜ΈμΆœν•˜κ³  κ²°κ³Όλ₯Ό μΊμ‹œμ— μ €μž₯
chat.predict("How do you make italian pasta")

# 두 번째 호좜: APIλ₯Ό ν˜ΈμΆœν•˜μ§€ μ•Šκ³  μΊμ‹œμ—μ„œ μ¦‰μ‹œ κ²°κ³Όλ₯Ό κ°€μ Έμ˜΄
chat.predict("How do you make italian pasta")

πŸ” μ½”λ“œ 상세 μ„€λͺ…

1. set_llm_cache
LangChain의 μ „μ—­ 섀정을 톡해 λͺ¨λ“  LLM ν˜ΈμΆœμ— λŒ€ν•œ μΊμ‹œλ₯Ό ν™œμ„±ν™”ν•©λ‹ˆλ‹€. λ™μΌν•œ λͺ¨λΈμ— λ™μΌν•œ μž…λ ₯이 μ£Όμ–΄μ§€λ©΄, μ‹€μ œ APIλ₯Ό ν˜ΈμΆœν•˜λŠ” λŒ€μ‹  μΊμ‹œμ— μ €μž₯된 κ²°κ³Όλ₯Ό μ¦‰μ‹œ λ°˜ν™˜ν•©λ‹ˆλ‹€.

  • InMemoryCache: μΊμ‹œλ₯Ό λ©”λͺ¨λ¦¬μ— μ €μž₯ν•©λ‹ˆλ‹€. μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ΄ μ’…λ£Œλ˜λ©΄ μΊμ‹œλ„ μ‚¬λΌμ§‘λ‹ˆλ‹€. κ°„λ‹¨ν•œ ν…ŒμŠ€νŠΈμ— μœ μš©ν•©λ‹ˆλ‹€.

  • SQLiteCache: μΊμ‹œλ₯Ό 파일(cache.db)에 μ €μž₯ν•©λ‹ˆλ‹€. μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ„ μž¬μ‹œμž‘ν•΄λ„ μΊμ‹œκ°€ μœ μ§€λ©λ‹ˆλ‹€. 영ꡬ적인 μΊμ‹œκ°€ ν•„μš”ν•  λ•Œ μ‚¬μš©ν•©λ‹ˆλ‹€.

  • μ™œ μ‚¬μš©ν•˜λŠ”κ°€?: 반볡적인 API ν˜ΈμΆœμ„ 쀄여 λΉ„μš©μ„ μ ˆμ•½ν•˜κ³ , μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ˜ 응닡 속도λ₯Ό 크게 ν–₯μƒμ‹œν‚¬ 수 μžˆμŠ΅λ‹ˆλ‹€. 특히 개발 및 ν…ŒμŠ€νŠΈ λ‹¨κ³„μ—μ„œ 맀우 μœ μš©ν•©λ‹ˆλ‹€.

βœ… 체크리슀트

  • set_llm_cacheλ₯Ό importν•˜κ³  SQLiteCacheλ₯Ό μ‚¬μš©ν•˜μ—¬ μΊμ‹œλ₯Ό μ„€μ •ν–ˆλ‚˜μš”?
  • λ™μΌν•œ predict ν˜ΈμΆœμ„ 두 번 μ‹€ν–‰ν•˜μ—¬ 두 번째 호좜이 훨씬 λΉ λ₯Έ 것을 ν™•μΈν–ˆλ‚˜μš”?
  • cache.db 파일이 μƒμ„±λœ 것을 ν™•μΈν–ˆλ‚˜μš”?

Serialization (Usage & Model)

🎯 이번 λ‹¨κ³„μ—μ„œ 배울 것

  • get_openai_callback을 μ‚¬μš©ν•˜μ—¬ 토큰 μ‚¬μš©λŸ‰κ³Ό API λΉ„μš©μ„ μΆ”μ ν•˜λŠ” 방법
  • μ±— λͺ¨λΈμ˜ 섀정을 .json 파일둜 μ €μž₯ν•˜κ³  λΆˆλŸ¬μ˜€λŠ” 방법

πŸ“ 1단계: μ‚¬μš©λŸ‰ 좔적 및 λͺ¨λΈ 직렬화

전체 μ½”λ“œ (notebook.ipynb):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from langchain.callbacks import get_openai_callback
from langchain.chat_models import ChatOpenAI

chat = ChatOpenAI(temperature=0.1)

# μ‚¬μš©λŸ‰ 좔적
with get_openai_callback() as usage:
a = chat.predict("What is the recipe for soju")
b = chat.predict("What is the recipe for bread")
print(usage)

# λͺ¨λΈ μ„€μ • μ €μž₯
chat.save("model.json")

# λͺ¨λΈ μ„€μ • 뢈러였기
from langchain.chat_models import load_model
loaded_model = load_model("model.json")

πŸ” μ½”λ“œ 상세 μ„€λͺ…

1. get_openai_callback
with ꡬ문과 ν•¨κ»˜ μ‚¬μš©ν•˜μ—¬ μ½”λ“œ 블둝 λ‚΄μ—μ„œ λ°œμƒν•˜λŠ” λͺ¨λ“  OpenAI API ν˜ΈμΆœμ— λŒ€ν•œ μƒμ„Έν•œ μ‚¬μš© 정보λ₯Ό 얻을 수 μžˆμŠ΅λ‹ˆλ‹€.

  • 좔적 정보: 총 μ‚¬μš© 토큰, ν”„λ‘¬ν”„νŠΈ 토큰, μ™„λ£Œ 토큰, μ„±κ³΅ν•œ μš”μ²­ 수, 총 λΉ„μš©(USD) 등을 μ œκ³΅ν•©λ‹ˆλ‹€.
  • μ™œ μ‚¬μš©ν•˜λŠ”κ°€?: μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ˜ API λΉ„μš©μ„ λͺ¨λ‹ˆν„°λ§ν•˜κ³  μ΅œμ ν™”ν•˜λŠ” 데 ν•„μˆ˜μ μž…λ‹ˆλ‹€.

2. λͺ¨λΈ 직렬화 (.save, load_model)
ChatOpenAI 객체에 μ„€μ •λœ temperature, model_name λ“±μ˜ νŒŒλΌλ―Έν„°λ“€μ„ .json 파일둜 μ €μž₯ν•˜κ±°λ‚˜ 뢈러올 수 μžˆμŠ΅λ‹ˆλ‹€. 이λ₯Ό 톡해 λͺ¨λΈ ꡬ성을 μ½”λ“œμ™€ λΆ„λ¦¬ν•˜μ—¬ 관리할 수 μžˆμŠ΅λ‹ˆλ‹€.

μ˜ˆμ‹œ (model.json):

1
2
3
4
5
{
"model_name": "gpt-3.5-turbo-16k",
"temperature": 0.1,
"_type": "openai"
}

βœ… 체크리슀트

  • get_openai_callback을 μ‚¬μš©ν•˜μ—¬ API 호좜의 λΉ„μš©κ³Ό 토큰 μ‚¬μš©λŸ‰μ„ 좜λ ₯ν•΄ λ³΄μ•˜λ‚˜μš”?
  • chat.save("model.json")을 μ‚¬μš©ν•˜μ—¬ λͺ¨λΈ 섀정을 μ €μž₯ν–ˆλ‚˜μš”?
  • load_model("model.json")을 μ‚¬μš©ν•˜μ—¬ μ €μž₯된 섀정을 λ‹€μ‹œ λΆˆλŸ¬μ™”λ‚˜μš”?

좜처 : https://nomadcoders.co/fullstack-gpt