■ StructuredTool 클래스의 ainvoke 메소드를 사용해 도구를 호출하는 방법을 보여준다.
※ OPENAI_API_KEY 환경 변수 값은 .env 파일에 정의한다.
▶ main.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import asyncio from dotenv import load_dotenv from langchain_core.prompts import ChatPromptTemplate from langchain_openai import ChatOpenAI from langchain_core.output_parsers import StrOutputParser from langchain_core.tools import tool load_dotenv() def reverse(text : str): return text[::-1] chatPromptTemplate = ChatPromptTemplate.from_template( "You are an expert writer. Summarize the following text in 10 words or less:\n\n{long_text}" ) chatOpenAI = ChatOpenAI(model = "gpt-4o-mini") strOutputParser = StrOutputParser() runnableSequence = chatPromptTemplate | chatOpenAI | strOutputParser | reverse @tool async def specialSummarizationTool(longText : str) -> str: """A tool that summarizes input text using advanced techniques.""" summary = await runnableSequence.ainvoke({"long_text" : longText}) return summary longText = """ NARRATOR: (Black screen with text; The sound of buzzing bees can be heard) According to all known laws of aviation, there is no way a bee should be able to fly. Its wings are too small to get its fat little body off the ground. The bee, of course, flies anyway because bees don't care what humans think is impossible. BARRY BENSON: (Barry is picking out a shirt) Yellow, black. Yellow, black. Yellow, black. Yellow, black. Ooh, black and yellow! Let's shake it up a little. JANET BENSON: Barry! Breakfast is ready! BARRY: Coming! Hang on a second. """ async def main(): responseString = await specialSummarizationTool.ainvoke({"longText" : longText}) print(responseString) asyncio.run(main()) |
▶ requirements.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
annotated-types==0.7.0 anyio==4.6.0 certifi==2024.8.30 charset-normalizer==3.3.2 colorama==0.4.6 distro==1.9.0 h11==0.14.0 httpcore==1.0.5 httpx==0.27.2 idna==3.10 jiter==0.5.0 jsonpatch==1.33 jsonpointer==3.0.0 langchain-core==0.3.6 langchain-openai==0.2.1 langsmith==0.1.129 openai==1.50.2 orjson==3.10.7 packaging==24.1 pydantic==2.9.2 pydantic_core==2.23.4 python-dotenv==1.0.1 PyYAML==6.0.2 regex==2024.9.11 requests==2.32.3 sniffio==1.3.1 tenacity==8.5.0 tiktoken==0.7.0 tqdm==4.66.5 typing_extensions==4.12.2 urllib3==2.2.3 |
※ pip install python-dotenv langchain-openai 명령을 실행했다.