반응형
블로그 이미지
개발자로서 현장에서 일하면서 새로 접하는 기술들이나 알게된 정보 등을 정리하기 위한 블로그입니다. 운 좋게 미국에서 큰 회사들의 프로젝트에서 컬설턴트로 일하고 있어서 새로운 기술들을 접할 기회가 많이 있습니다. 미국의 IT 프로젝트에서 사용되는 툴들에 대해 많은 분들과 정보를 공유하고 싶습니다.
솔웅

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

글 보관함

카테고리


반응형

https://python.langchain.com/docs/expression_language/cookbook/tools

 

Using tools | 🦜️🔗 Langchain

You can use any Tools with Runnables easily.

python.langchain.com

 

Using tools

 

You can use any Tools with Runnables easily.

 

Runnables와 함께 모든 도구를 쉽게 사용할 수 있습니다.

 

pip install duckduckgo-search

 

from langchain.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate
from langchain.schema.output_parser import StrOutputParser
from langchain.tools import DuckDuckGoSearchRun

 

template = """turn the following user input into a search query for a search engine:

{input}"""
prompt = ChatPromptTemplate.from_template(template)

model = ChatOpenAI()

 

 

chain = prompt | model | StrOutputParser() | search

 

chain.invoke({"input": "I'd like to figure out what games are tonight"})

 

    'What sports games are on TV today & tonight? Watch and stream live sports on TV today, tonight, tomorrow. Today\'s 2023 sports TV schedule includes football, basketball, baseball, hockey, motorsports, soccer and more. Watch on TV or stream online on ESPN, FOX, FS1, CBS, NBC, ABC, Peacock, Paramount+, fuboTV, local channels and many other networks. MLB Games Tonight: How to Watch on TV, Streaming & Odds - Thursday, September 7. Seattle Mariners\' Julio Rodriguez greets teammates in the dugout after scoring against the Oakland Athletics in a ... Circle - Country Music and Lifestyle. Live coverage of all the MLB action today is available to you, with the information provided below. The Brewers will look to pick up a road win at PNC Park against the Pirates on Wednesday at 12:35 PM ET. Check out the latest odds and with BetMGM Sportsbook. Use bonus code "GNPLAY" for special offers! MLB Games Tonight: How to Watch on TV, Streaming & Odds - Tuesday, September 5. Houston Astros\' Kyle Tucker runs after hitting a double during the fourth inning of a baseball game against the Los Angeles Angels, Sunday, Aug. 13, 2023, in Houston. (AP Photo/Eric Christian Smith) (APMedia) The Houston Astros versus the Texas Rangers is one of ... The second half of tonight\'s college football schedule still has some good games remaining to watch on your television.. We\'ve already seen an exciting one when Colorado upset TCU. And we saw some ...'

 

 

이 코드는 LangChain을 사용하여 DuckDuckGo 검색 엔진을 활용해 사용자 입력을 검색 쿼리로 변환하고 검색 결과를 반환하는 과정을 수행합니다. 아래는 코드의 주요 부분에 대한 설명입니다:

  1. pip install duckduckgo-search: 우선 duckduckgo-search 패키지를 설치합니다. 이 패키지는 DuckDuckGo 검색 엔진을 사용할 수 있게 해줍니다.
  2. from langchain.chat_models import ChatOpenAI: OpenAI의 언어 모델을 사용하기 위한 모듈을 가져옵니다.
  3. from langchain.prompts import ChatPromptTemplate: 대화식 프롬프트를 작성하기 위한 모듈을 가져옵니다.
  4. from langchain.schema.output_parser import StrOutputParser: 모델의 출력을 파싱하고 문자열로 반환하는 데 사용할 출력 파서를 가져옵니다.
  5. from langchain.tools import DuckDuckGoSearchRun: DuckDuckGo 검색을 수행하기 위한 모듈을 가져옵니다.
  6. template = """turn the following user input into a search query for a search engine: ... """: 대화 프롬프트의 템플릿을 정의합니다. 사용자 입력을 검색 엔진 쿼리로 변환할 것이며, 이 템플릿을 사용하여 프롬프트를 생성합니다.
  7. prompt = ChatPromptTemplate.from_template(template): 정의한 템플릿을 사용하여 대화 프롬프트를 생성합니다.
  8. model = ChatOpenAI(): ChatOpenAI 모델을 초기화합니다. 이 모델은 사용자 입력을 검색 쿼리로 변환하는 역할을 수행합니다.
  9. chain = prompt | model | StrOutputParser() | search: 체인을 구성합니다. prompt 객체를 사용자 입력과 함께 모델 model에 전달한 후, 출력을 문자열로 파싱하고 최종적으로 search 객체를 사용하여 DuckDuckGo 검색을 수행합니다.
  10. chain.invoke({"input": "I'd like to figure out what games are tonight"}): "I'd like to figure out what games are tonight"와 같은 사용자 입력을 포함한 입력 딕셔너리를 체인에 전달하여 실행합니다. 결과적으로, 이 입력 문장이 DuckDuckGo 검색 쿼리로 변환되고, 검색 결과가 반환됩니다.

이를 통해 사용자의 입력이 검색 쿼리로 변환되어 DuckDuckGo에서 관련 정보를 검색하는 과정이 자동화됩니다.

 

 

이 예제를 로컬에서 돌렸더니 답을 얻지를 못했습니다.

어쨌던 다른 외부의 tool을 사용하는 방법은 chain에 그것을 넣어주면 되는군요.

 

 

 

 

반응형