Skip to main content

Helper and utility functions for langchain

Project description

langchain-teddynote

랭체인 한국어 튜토리얼에 사용되는 다양한 유틸 파이썬 패키지.

LangChain 을 사용하면서 불편한 기능이나, 추가적인 기능을 제공합니다.

설치

pip install langchain-teddynote

사용법

스트리밍 출력

스트리밍 출력을 위한 stream_response 함수를 제공합니다.

from langchain_teddynote.messages import stream_response
from langchain_openai import ChatOpenAI

# 객체 생성
llm = ChatOpenAI(
    temperature=0.1,  # 창의성 (0.0 ~ 2.0)
    model_name="gpt-4o",  # 모델명
)
answer = llm.stream("대한민국의 아름다운 관장지 10곳과 주소를 알려주세요!")

# 스트리밍 출력만 하는 경우
stream_response(answer)

# 출력된 답변을 반환 값으로 받는 경우
# final_answer = stream_response(answer, return_output=True)

LangSmith 추적

# LangSmith 추적을 설정합니다. https://smith.langchain.com
# 환경변수 설정은 되어 있다고 가정합니다.
from langchain_teddynote import logging

# 프로젝트 이름을 입력합니다.
logging.langsmith("프로젝트명 기입")

출력

LangSmith 추적을 시작합니다.
[프로젝트명]
(기입한 프로젝트명)

멀티모달 모델(이미지 입력)

from langchain_teddynote.models import MultiModal
from langchain_teddynote.messages import stream_response

# 객체 생성
llm = ChatOpenAI(
    temperature=0.1,  # 창의성 (0.0 ~ 2.0)
    model_name="gpt-4o",  # 모델명
)

# 멀티모달 객체 생성
system_prompt = """당신은 표(재무제표) 를 해석하는 금융 AI 어시스턴트 입니다. 
당신의 임무는 주어진 테이블 형식의 재무제표를 바탕으로 흥미로운 사실을 정리하여 친절하게 답변하는 것입니다."""

user_prompt = """당신에게 주어진 표는 회사의 재무제표 입니다. 흥미로운 사실을 정리하여 답변하세요."""

# 멀티모달 객체 생성
multimodal_llm = MultiModal(
    llm, system_prompt=system_prompt, user_prompt=user_prompt
)

# 샘플 이미지 주소(웹사이트로 부터 바로 인식)
IMAGE_URL = "https://storage.googleapis.com/static.fastcampus.co.kr/prod/uploads/202212/080345-661/kwon-01.png"

# 로컬 PC 에 저장되어 있는 이미지의 경로 입력
# IMAGE_URL = "./images/sample-image.png"

# 이미지 파일로 부터 질의
answer = multimodal_llm.stream(IMAGE_URL)
# 스트리밍 방식으로 각 토큰을 출력합니다. (실시간 출력)
stream_response(answer)

DeepL 번역기

from langchain_teddynote.translate import Translator

# api키 설정
deepl_api_key = os.getenv("DEEPL_API_KEY")

# 번역 객체 생성(source_lang, target_lang)
translator = Translator(deepl_api_key, "EN", "KO")

# 번역 실행
translated_text = translator("hello, nice to meet you")
print(translated_text)

Kiwi 형태소 분석기

from langchain_teddynote.community.kiwi_tokenizer import KiwiTokenizer

# 토크나이저 선언
kiwi_tokenizer = KiwiTokenizer()

sent1 = "안녕하세요. 반갑습니다. 내 이름은 테디입니다."
sent2 = "안녕하세용 반갑습니다~^^ 내 이름은 테디입니다!!"

# 토큰화
print(kiwi_tokenizer.tokenize(sent1))
print(kiwi_tokenizer.tokenize(sent2))

Synapsoft DocuAnalyzer

from langchain_teddynote.document_parser import SynapsoftDocuAnalyzer

api = SynapsoftDocuAnalyzer(api_key="API_KEY 를 입력해 주세요")

# markdown 형식으로 변환(반환 형식: List[str])
markdown = api.convert_to_markdown("sample.pdf")

# xml 형식으로 변환(반환 형식: List[str])
xml = api.convert_to_xml("sample.pdf")

# json 형식으로 변환(반환 형식: List[str])
json = api.convert_to_json("sample.pdf")

OpenAI Assistant V2

from langchain_teddynote.models import OpenAIAssistant


# RAG 시스템 프롬프트 입력
_DEFAULT_RAG_INSTRUCTIONS = """You are an assistant for question-answering tasks. 
Use the following pieces of retrieved context to answer the question. 
If you don't know the answer, just say that you don't know. 
Answer in Korean."""


# 설정(configs)
configs = {
    "OPENAI_API_KEY": openai_api_key,  # OpenAI API 키
    "instructions": _DEFAULT_RAG_INSTRUCTIONS,  # RAG 시스템 프롬프트
    "PROJECT_NAME": "PDF-RAG-TEST",  # 프로젝트 이름(자유롭게 설정)
    "model_name": "gpt-4o",  # 사용할 OpenAI 모델 이름(gpt-4o, gpt-4o-mini, ...)
    "chunk_size": 1000,  # 청크 크기
    "chunk_overlap": 100,  # 청크 중복 크기
}


# 인스턴스 생성
assistant = OpenAIAssistant(configs)

# 업로드할 파일 경로
data = "파일이름.pdf"

# 파일 업로드 후 file_id 는 잘 보관해 두세요. (대시보드에서 나중에 확인 가능)
file_id = assistant.upload_file(data)

# 업로드한 파일의 ID 리스트 생성
file_ids = [file_id]

# 새로운 어시스턴트 생성 및 ID 받기
assistant_id, vector_id = assistant.create_new_assistant(file_ids)

# 어시스턴트 설정
assistant.setup_assistant(assistant_id)

# 벡터 스토어 설정
assistant.setup_vectorstore(vector_id)

스트리밍 출력

for token in assistant.stream("삼성전자가 개발한 생성형 AI의 이름은?"):
    print(token, end="", flush=True)

혹은

from langchain_teddynote.messages import stream_response

stream_response(assistant.stream("이전 답변을 영어로"))

일반 출력

# 질문
print(assistant.invoke("삼성전자가 개발한 생성형 AI의 이름은?"))

대화 목록을 조회

# 대화 목록 조회
assistant.list_chat_history()

대화 초기화

# 대화 초기화
assistant.clear_chat_history()

튜토리얼

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langchain_teddynote-0.4.1.tar.gz (43.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langchain_teddynote-0.4.1-py3-none-any.whl (57.1 kB view details)

Uploaded Python 3

File details

Details for the file langchain_teddynote-0.4.1.tar.gz.

File metadata

  • Download URL: langchain_teddynote-0.4.1.tar.gz
  • Upload date:
  • Size: 43.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for langchain_teddynote-0.4.1.tar.gz
Algorithm Hash digest
SHA256 ee341805627c0bcbba8ad92da914f9528b7008aeb9ac0e8b1e41d433335acec0
MD5 6224ae5c497396358d6b41c0b946b841
BLAKE2b-256 1268b1c72b05675f3f7fcb54528b6191a24e08ef19257620b1a10245edd65570

See more details on using hashes here.

File details

Details for the file langchain_teddynote-0.4.1-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_teddynote-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a32a54d14d20d1663a6b5d03593ef3731d1aa8c5b4ad320cef90e845883be16c
MD5 654633f937cf6c903e8778a782fefd73
BLAKE2b-256 736d6b60c7e937aadb5f45bc823fbe1f7619195be8a715765268b759f577c45f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page