■ StructuredTool 클래스의 astream_events 메소드를 사용해 "on_chat_model_end" 이벤트를 수신하는 방법을 보여준다.
※ 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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
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(): asyncGenerator = specialSummarizationTool.astream_events({"longText" : longText}, version = "v2") async for eventDictionary in asyncGenerator: if eventDictionary["event"] == "on_chat_model_end": print(eventDictionary) asyncio.run(main()) """ { 'event' : 'on_chat_model_end', 'data' : { 'output' : AIMessage( content = 'Bee defies aviation laws; Barry prepares for breakfast.', additional_kwargs = {}, response_metadata = { 'finish_reason' : 'stop', 'model_name' : 'gpt-4o-mini-2024-07-18', 'system_fingerprint' : 'fp_f85bea6784' }, id = 'run-1b4d203f-eceb-4507-a321-12189a0b52da' ), 'input' : { 'messages' : [ [ HumanMessage( content = "You are an expert writer. Summarize the following text in 10 words or less:\n\n\nNARRATOR:\n(Black screen with text; The sound of buzzing bees can be heard)\nAccording 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.\nBARRY BENSON:\n(Barry is picking out a shirt)\nYellow, black. Yellow, black. Yellow, black. Yellow, black. Ooh, black and yellow! Let's shake it up a little.\nJANET BENSON:\nBarry! Breakfast is ready!\nBARRY:\nComing! Hang on a second.\n", additional_kwargs = {}, response_metadata={} ) ] ] } }, 'run_id' : '1b4d203f-eceb-4507-a321-12189a0b52da', 'name' : 'ChatOpenAI', 'tags' : ['seq:step:2'], 'metadata' : { 'ls_provider' : 'openai', 'ls_model_name' : 'gpt-4o-mini', 'ls_model_type' : 'chat', 'ls_temperature' : 0.7 }, 'parent_ids' : [ '243969e3-7dee-42a0-9b62-278a42759950', '1d8806b0-e9b1-46dc-9a9f-d5d84791b935' ] } """ |
▶ 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 명령을 실행했다.