AI - LangChain(Model I/O) 학습내용 (2)

KyungminLee·2024년 4월 8일

Chatgpt

목록 보기
3/4
post-thumbnail

1. Modules

LangChain은 다음과 같은 주요 모듈을 위한 확장 가능한 표준 인터페이스 및 외부 통합 기능을 제공합니다:

  • Model I/O Callbacks
    대규모 언어 모델과의 I/O 인터페이스 생성하고, 모든 체인의 시스템 기록 및 스트리밍 등 상태 확인할 수 있는 기능입니다.
  • Chains
    함께 사용될 때 강력한 효과를 내는 작은 모델들의 조합입니다. 예를 들어, 첫 번째 모델이 텍스트를 생성하고, 두 번째 모델이 그 텍스트를 평가하고, 세 번째 모델이 해당 텍스트를 기반으로 질문에 답하는 과정을 포함할 수 있습니다.
  • Memory ⭐️
    언어 모델이 이전에 접근했던 컨텍스트 정보를 저장하고 검색할 수 있는 메커니즘입니다. 이를 통해 언어 모델은 이전에 접근했던 정보를 회상하고 새로운 정보를 추론할 수 있습니다.
  • Retrieval
    언어 모델이 대규모 텍스트 데이터베이스에서 관련성 있는 정보를 검색하고 선택할 수 있는 기능입니다. 이를 통해 언어 모델은 현재 문맥에 관련성 높은 정보를 자동으로 찾아내고 활용할 수 있습니다.
  • Agents
    특정 작업을 수행하도록 미션을 부여하거나 특정 목표를 달성하도록 하는 등의 작업에 사용되는 소프트웨어 요소입니다. 예를 들어, 높은 수준의 지침이 주어지면 체인이 어떤 도구를 사용할지 알아서 선택 합니다.

2. Model IO

FewShotPromptTemplate

몇 개의 예제를 포함하는 단일 프롬프트 템플릿.

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"})

FewShotChatMessagePromptTemplate

몇 개의 예제를 포함하는 채팅 프롬프트 템플릿.

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"})

LengthBasedExampleSelector, BaseExampleSelector

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")

Serialization and Composition⭐️

여러 프롬프트를 함께 구성하는 방법에 대해 설명합니다. 프롬프트의 일부를 재사용하려는 경우에 유용합니다. 이 방법은 파이프라인 프롬프트를 사용하여 수행할 수 있습니다. 파이프라인 프롬프트는 크게 두 부분으로 구성됩니다:

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

Caching⭐️

새 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초만에 동일한 대답을 뱉는다.

Serialization⭐️

  1. Token Information 확인하기 get_openai_callback
  2. 모델 정보 저장하기 (TO JSON File) save
  3. 저장한 모델 정보 로드하기 load_llm
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
profile
끊임없이 발전해가는 개발자.

0개의 댓글