
LangChain은 다음과 같은 주요 모듈을 위한 확장 가능한 표준 인터페이스 및 외부 통합 기능을 제공합니다:
몇 개의 예제를 포함하는 단일 프롬프트 템플릿.
Prompt template that contains few shot examples.
examples = [
{
"question": "What do you know about France?",
"answer": """
Here is what I know:
Capital: Paris
Language: French
Food: Wine and Cheese
Currency: Euro
""",
},
{
"question": "What do you know about Italy?",
"answer": """
I know this:
Capital: Rome
Language: Italian
Food: Pizza and Pasta
Currency: Euro
""",
},
{
"question": "What do you know about Greece?",
"answer": """
I know this:
Capital: Athens
Language: Greek
Food: Souvlaki and Feta Cheese
Currency: Euro
""",
}
]
# 형식화
examples_template = """
Human: {question}
AI: {answer}
"""
# 프롬프트 생성
example_prompt = PromptTemplate.from_template(examples_template)
# FewShotPromptTemplate로 전달
# suffix: A prompt template string to put after the examples.
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":"Korea"})
몇 개의 예제를 포함하는 채팅 프롬프트 템플릿.
Chat prompt template that supports few-shot examples.
example_prompt = ChatPromptTemplate.from_messages(
[
("human", "What do you know about {country}?"),
("ai", "{answer}"),
]
)
# Chat prompt template that supports few-shot examples.
example_few_shot_prompt = FewShotChatMessagePromptTemplate(
example_prompt=example_prompt,
examples=examples,
)
final_prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a geography expert, you give short answers."),
example_few_shot_prompt,
("human", "What do you know about {country}?"),
]
)
chain = final_prompt | chat
chain.invoke({"country": "Thailand"})
BaseExampleSelector: Interface for selecting examples to include in prompts. 프롬프트에 포함할 예제를 선택하기 위한 인터페이스입니다.
LengthBasedExampleSelector: Select examples based on length. 길이를 기준으로 예제를 선택합니다.
# BaseExampleSelector: Interface for selecting examples to include in prompts.
# RandomExampleSelector: 여러 examples 중 랜덤으로 하나 뽑는 my_customizing 함수
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)]
# Select examples based on length.
example_selector_length = LengthBasedExampleSelector(
examples=examples,
example_prompt=example_prompt,
max_length=80
)
# Interface for selecting examples to include in prompts.
example_selector_random = RandomExampleSelector(
examples=examples,
)
# Prompt template that contains few shot examples.
prompt = FewShotPromptTemplate(
example_prompt=example_prompt,
example_selector=example_selector_random,
suffix="Human: What do you know about {country}?",
input_variables=["country"],
)
prompt.format(country="Brazil")
여러 프롬프트를 함께 구성하는 방법에 대해 설명합니다. 프롬프트의 일부를 재사용하려는 경우에 유용합니다. 이 방법은 파이프라인 프롬프트를 사용하여 수행할 수 있습니다. 파이프라인 프롬프트는 크게 두 부분으로 구성됩니다:
final_prompt: 반환되는 최종 프롬프트.
pipeline_prompts: 문자열 이름과 프롬프트 템플릿으로 구성된 튜플 목록입니다. 각 프롬프트 템플릿은 형식이 지정된 다음 동일한 이름의 변수로 향후 프롬프트 템플릿에 전달됩니다.
https://python.langchain.com/docs/modules/model_io/prompts/pipeline
from langchain.chat_models import ChatOpenAI
from langchain.callbacks import StreamingStdOutCallbackHandler
from langchain.prompts import load_prompt
from langchain.prompts.pipeline import PipelinePromptTemplate
chat = ChatOpenAI(temperature=0.1, streaming=True, callbacks=[StreamingStdOutCallbackHandler()])
# 분리된 프롬프트 불러오기 - loading a prompt from LangChainHub or local fs. (json file, yaml file)
prompt = load_prompt('./prompt.yaml')
prompt.format(country="China")
intro = PromptTemplate.from_template(
"""
You are a role playing assistant.
And you are impersonating a {character}
"""
)
example = PromptTemplate.from_template(
"""
This is an example of how you talk:
Human: {example_question}
You: {example_answer}
"""
)
start = PromptTemplate.from_template(
"""
Start now!
Human: {question}
You:
"""
)
final = PromptTemplate.from_template(
"""
{intro}
{example}
{start}
"""
)
prompts = [
("intro", intro),
("example", example),
("start", start),
]
# PipelinePromptTemplate: prompt template for composing multiple prompt templates together.
# 리액트 컴토넌트처럼 프롬프트들을 재사용할 때 유용하다. 하나의 메인 프롬프트(final_prompt)가 존재하고, 나머지 자잘한 프롬프트들을 파이프 라인으로 이어준다.
full_prompt = PipelinePromptTemplate(final_prompt=final, pipeline_prompts=prompts)
chain = full_prompt | chat
chain.invoke({
"character":"Cat",
"example_question":"what is your location?",
"example_answer":"Meow Meow! that is secret! Meow Meow!",
"question":'what is your favorite food?'
}).content
새 LLM 캐시를 설정하여 이전 값(있는 경우)을 덮어씁니다.
메모리 또는 SQLite에 주로 저장합니다.
Set a new LLM cache, overwriting the previous value, if any.
Cache that stores things in memory or SQLite as a backend .
from langchain.globals import set_llm_cache, set_debug
from langchain.cache import InMemoryCache, SQLiteCache
set_llm_cache(InMemoryCache()) # 똑같은 질문을 계속 하면 답변을 재사용하여 비용을 절약하는 기법 (인메모리 캐싱)
set_llm_cache(SQLiteCache('cache.db')) # cache.db 라는 데이터베이스를 만들어 저장한다.
set_debug(True) # 어떻게 작업중인지 로깅하여 확인 가능. 체인 작업 시 용이
chat = ChatOpenAI(temperature=0.1)
chat.predict('how do you make tomato pasta')
# 처음에는 모두 답변하기까지 X초가 걸리지만 캐싱 기법을 적용하면 같은 질문에 대해 0초만에 동일한 대답을 뱉는다.
from langchain.cache import InMemoryCache, SQLiteCache
from langchain.globals import set_llm_cache, set_debug
set_llm_cache(InMemoryCache()) # 똑같은 질문을 계속 하면 답변을 재사용하여 비용을 절약하는 기법 (인메모리 캐싱)
set_llm_cache(SQLiteCache('cache.db')) # cache.db 라는 데이터베이스를 만들어 저장한다.
set_debug(False) # 어떻게 작업중인지 로깅하여 확인 가능. 체인 작업 시 용이
from langchain.callbacks import get_openai_callback # about token and cost information.
with get_openai_callback() as usage:
a = chat.predict('WHat this the recipe for jelly')
b = chat.predict('WHat this the recipe for bread')
print(a,b, "\\n")
print(usage.completion_tokens)
from langchain.chat_models import ChatOpenAI
from langchain.llms.openai import OpenAI
# chatModel json 파일로 저장
chat = OpenAI(
temperature=0.1,
max_tokens=450,
model='gpt-3.5-turbo-16k'
)
chat.save('model.json')
# 저장한 model 로드해서 사용하기
from langchain.llms.loading import load_llm
chat = load_llm('model.json')
chat