python 55

[GPT] ConversationBufferMemory

ConversationBufferMemory ConversationBufferMemory는 메시지를 저장한 다음 변수에서 메시지를 추출합니다. 단순히 이전 대화 내용 전체를 저장 대화 내용이 길어질수록 메모리도 계속 커져서 비효율적 [결과] {'history': 'Human: Hi!\nAI: How are you?'} ConversationBufferMemory(return_messages=True)를 이용하여 결과를 문자열이 아닌 리스트 형태로 받을수 있음 [chat model 사용시 유용] [결과] {'history': [HumanMessage(content='Hi!'), AIMessage(content='How are you?')]}

python 2024.03.29

[GPT] Caching

캐싱을 사용하면 언어모델의 응답을 저장할 수 있음 이후 동일 질문을 받을경우 언어모델에 질의를 하지 않고 캐싱된 답변을 가져옴 set_llm_cache 새 LLM 캐시 설정, 이전 값 덮어쓰기(있는 경우) 캐시 종류 InMemoryCache: 메모리를 이용하여 캐시를 저장. 서버 재실행시 캐싱된 내용이 사라짐 SQLite Cache: SQLite cache를 이용하여 캐시를 저장 그외에도 여러가지의 캐시가 있음 https://python.langchain.com/docs/integrations/llms/llm_caching#in-memory-cache LLM Caching integrations | 🦜️🔗 Langchain This notebook covers how to cache results of ..

python 2024.03.28

[GPT] PipelinePromptTemplate

여러개의 prompt를 하나로 합쳐주는 기능 prompt를 재사용하는데 유용함 pipeline prompt 는 크게 두부분으로 구성 Final prompt: 최종 프롬프트 반환 Pipeline prompts: 문자열 이름과 프롬프트 템플릿으로 구성된 튜플 리스트 각 prompt template은 이미 format이 완료된 상태이며 동일 변수명으로 prompt template 에 전달된다. [결과값] "You are impersonating Elon Musk.\n\nHere's an example of an interaction:\n\nQ: What's your favorite car?\nA: Tesla\n\nNow, do this for real!\n\nQ: What's your favorite soci..

python 2024.03.28

[GPT] LengthBasedExampleSelector

LengthBasedExampleSelector 텍스트 길이를 기준으로 FewShotPromptTemplate 사용시 제시되는 example 들 중에 원하는 example을 선택할수 있음 [결과] 'Human: What do you know about France?\nAI: \n Here is what I know:\n Capital: Paris\n Language: French\n Food: Wine and Cheese\n Currency: Euro\n \n\nHuman: What do you know about Italy?\nAI: \n I know this:\n Capital: Rome\n Language: Italian\n Food: Pizza and Pasta\n Currency: Euro\n ..

python 2024.03.28

[GPT] Few-shot examples for chat models

[결과] AIMessageChunk(content='A triangle does not have a square. The square of a number is the result of multiplying the number by itself.') 템플릿의 기본 구성 요소는 다음과 같습니다. - examples: 최종 프롬프트에 포함할 사전 예제 목록입니다. - example_prompt: format_messages 메서드를 통해 각 예제를 하나 이상의 메시지로 변환합니다. 일반적인 예는 각 예를 하나의 인간 메시지와 하나의 AI 메시지 응답으로 변환하거나 인간 메시지와 함수 호출 메시지로 변환하는 것입니다.

python 2024.03.27

[GPT] FewShotPromptTemplate

[결과] AI: I know this: Capital: Ankara Language: Turkish Food: Kebab and Baklava Currency: Turkish Lira AIMessageChunk(content='AI:\n I know this:\n Capital: Ankara\n Language: Turkish\n Food: Kebab and Baklava\n Currency: Turkish Lira') FewShotPromptTemplate은 GPT-3나 GPT-4 같은 모델에 예제를 제공하고 예제를 기반으로 더 나은 응답을 출력할수 있도록 함 prompt = FewShotPromptTemplate( example_prompt=example_prompt, // 예제 프롬프트 exampl..

python 2024.03.27

[GPT] OutputParser and LCEL

[결과값] ['mercury', 'venus', 'earth', 'mars', 'jupiter', 'saturn', 'uranus', 'neptune'] BaseOutputParser LLM 호출의 출력을 구문 분석하는 기본 클래스 parse 메서드는 추상 메서드임 parse를 하기위한 기본 메서드 CommaOutputParser(BaseOutputParser) CommaOutputParser 클레스에 BaseOutputParser 클래스를 상속시킴 위의 코드를 아래처럼 LCEL(LangChain expression language)형태로 변경할 수 있음LCEL은 다양한 template 와 LLM 호출, response를 사용하게 해줌 [결과값] ['pikachu', 'charmander', 'bulba..

python 2024.03.26