본문 바로가기
🤖 AI/LangChain - GPT 강의

🦜 풀스택 GPT - LangChain

by 정람지 2024. 2. 17.

🦜가상환경

>>> source env/bin/activate

>>> deactivate

🦜openAi 모델 종류

https://platform.openai.com/docs/models/overview


🦜 랭체인(LangChain)이란?

 

- 대규모 언어 모델과 애플리케이션의 통합을 간소화하는 SDK

- LLM과 상호 작용하는 탁월한 도구

 

- 외부 도구와 통합되어 환경을 조성하는 강력한 프레임워크

 

랭체인은 간단하고 통합된 API를 노출하여 기본 LLM의 구현 세부 사항을 요약하는데, 이 API를 통해 개발자들은 코드를 크게 변경하지 않고 모델을 쉽게 교체하거나 대체할 수 있다.

 

 

Introduction | 🦜️🔗 Langchain

LangChain is a framework for developing applications powered by language models. It enables applications that:

python.langchain.com


🦜LLM chat model 

chatOpenAI의 gpt-3.5-turbo-1106 모델 사용

 

모델이 엄청 빨리 바뀌는 것 같다..

There are 8 recognized planets in our solar system: Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, and Neptune. However, there is ongoing debate around the classification of Pluto as a planet, with some considering it a dwarf planet.

🦜Model constructure(생성자)

해당 모델의 상세 기능 설정 가능!

 

max_tokens : 모델이 반환하는 결과의 최대 토큰

temperature : 모델의 창의성 정도

 

 

 

🦜Predict Messages

메세지를 예측하기

 

message constructor를 import하기

 

humanMessage

AIMessage : AI에 의해 보내지는 메세지

SystemMesseage : LLM에 설정들을 제공하기 위한 메세지 

predict_messages


🦜Prompt Templates

langchain.prompt 의 PromptTemplate/ChatPromptTemplate

 

 

PromptTemplate : 스트링을 이용해 템플릿을 만듦

 

ChatPromptTemplate : 메세지로부터 템플릿을 만듦

문자열 템플릿

모델한테 넘기기

템플릿 개선!


🦜OutputParser

=> 모델의 응답 변형 시에 사용

데이터베이스 형태 또는 딕셔너리,  등 "자연어"의 형태가 아니게

 

parse 메서드를 구현해야 함!

잘라보기

와 리스트 형태의 응답이 반환!

 

이쁘게 "

answered with a comma seperated list

"

오오앙

만든 commaoutputPerser 이용하면

쨔잔


🦜LCEL

Langchain expression language

 

chat Model, OutputParser,template만 가지고 위의 결과물 만들기

=> chat Model, OutputParser,template을 묶은 chain 만들기

chain = template | chat | CommaOutputParser()

 

(순서 지켜야함

 

체인 여러 개로 묶어서도 가능

10원정도 소비ㅋㅋ


🦜Chaining Chains

AIMessageChunk(content="As a vegetarian chef, I can help you make a vegetarian version of Chicken Tikka Masala by replacing the chicken with a suitable alternative. In this case, you can use paneer, a type of Indian cottage cheese that is commonly used in vegetarian dishes. Here's how you can adapt the recipe:\n\nIngredients:\n- 1 lb paneer, cut into bite-sized pieces\n- 1 cup plain yogurt\n- 2 tbsp lemon juice\n- 2 tbsp vegetable oil\n- 1 onion, finely chopped\n- 3 cloves garlic, minced\n- 1-inch piece of ginger, grated\n- 1 can (14 oz) tomato sauce\n- 1 tbsp garam masala\n- 1 tsp ground cumin\n- 1 tsp ground coriander\n- 1/2 tsp turmeric\n- 1/2 tsp paprika\n- 1/2 tsp cayenne pepper (adjust to taste)\n- Salt and pepper to taste\n- Fresh cilantro, chopped (for garnish)\n- Cooked rice or naan bread (for serving)\n\nInstructions:\n1. Follow the same marinating process as the original recipe, but instead of chicken, marinate the paneer in the yogurt, lemon juice, vegetable oil, garlic, ginger, and salt. Refrigerate for at least 1 hour.\n\n2. Preheat the oven and bake the marinated paneer on skewers as you would with the chicken.\n\n3. In the skillet, sauté the onion, garlic, and ginger as directed in the recipe.\n\n4. Add the tomato sauce and spices to the skillet and simmer to create the sauce.\n\n5. Add the baked paneer to the sauce and simmer for a few more minutes to allow the flavors to blend.\n\n6. Serve the Paneer Tikka Masala over rice or with naan bread, garnished with fresh cilantro.\n\nEnjoy your vegetarian twist on this classic Indian dish!")

 

와 신기!


🦜Streaming

답변 생성 현황을 받아보고 싶다면 

from langchain.callbacks import StreamingStdOutCallbackHandler

그리고 챗봇 설정에 추가

streaming = True,
callbacks = [
StreamingStdOutCallbackHandler(),
],

 

그리고 이렇게 쭉! 나온다


ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ
남은 방이 없었던 키파언니를위해집무실 하나.. 여행자 협씨는 위에서부터 세상을 탐방중
세계여행멈춘여행자..여왕님의집사취직..운영진특혜!
성공적ㄷ!