■ get_openai_callback 함수를 사용해 에이전트에서 토큰 사용량을 구하는 방법을 보여준다.
※ 비용 정보에 대해 AgentExecutor 클래스 생성자에서 stream_runnable = False를 설정해야 한다.
※ 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 |
from dotenv import load_dotenv from langchain_openai import ChatOpenAI from langchain_community.agent_toolkits.load_tools import load_tools from langchain_core.prompts import ChatPromptTemplate from langchain.agents import create_tool_calling_agent from langchain.agents import AgentExecutor from langchain_community.callbacks.manager import get_openai_callback load_dotenv() chatOpenAI = ChatOpenAI(model = "gpt-3.5-turbo-0125", temperature = 0) toolList = load_tools(["wikipedia"]) chatPromptTemplate = ChatPromptTemplate.from_messages( [ ("system" , "You're a helpful assistant"), ("human" , "{input}"), ("placeholder", "{agent_scratchpad}") ] ) agentRunnableSequence = create_tool_calling_agent(chatOpenAI, toolList, chatPromptTemplate) agentExecutor = AgentExecutor(agent = agentRunnableSequence, tools = toolList, verbose = True, stream_runnable = False) with get_openai_callback() as generatorContextManager: responseDictionary = agentExecutor.invoke({"input" : "What's a hummingbird's scientific name and what's the fastest bird species?"}) print(f"Total Tokens : {generatorContextManager.total_tokens}" ) print(f"Prompt Tokens : {generatorContextManager.prompt_tokens}" ) print(f"Completion Tokens : {generatorContextManager.completion_tokens}") print(f"Total Cost (USD) : ${generatorContextManager.total_cost}" ) """ > Entering new AgentExecutor chain... Invoking: `wikipedia` with `{'query': 'Hummingbird scientific name'}` Page: Hummingbird Summary: Hummingbirds are birds native to the Americas and comprise the biological family Trochilidae. With approximately 366 species and 113 genera, they occur from Alaska to Tierra del Fuego, but most species are found in Central and South America. As of 2024, 21 hummingbird species are listed as endangered or critically endangered, with numerous species declining in population. Hummingbirds have varied specialized characteristics to enable rapid, maneuverable flight: exceptional metabolic capacity, adaptations to high altitude, sensitive visual and communication abilities, and long-distance migration in some species. Among all birds, male hummingbirds have the widest diversity of plumage color, particularly in blues, greens, and purples. Hummingbirds are the smallest mature birds, measuring 7.5–13 cm (3–5 in) in length. The smallest is the 5 cm (2.0 in) bee hummingbird, which weighs less than 2.0 g (0.07 oz), and the largest is the 23 cm (9 in) giant hummingbird, weighing 18–24 grams (0.63–0.85 oz). Noted for long beaks, hummingbirds are specialized for feeding on flower nectar, but all species also consume small insects. They are known as hummingbirds because of the humming sound created by their beating wings, which flap at high frequencies audible to other birds and humans. They hover at rapid wing-flapping rates, which vary from around 12 beats per second in the largest species to 80 per second in small hummingbirds. Hummingbirds have the highest mass-specific metabolic rate of any homeothermic animal. To conserve energy when food is scarce and at night when not foraging, they can enter torpor, a state similar to hibernation, and slow their metabolic rate to 1⁄15 of its normal rate. While most hummingbirds do not migrate, the rufous hummingbird has one of the longest migrations among birds, traveling twice per year between Alaska and Mexico, a distance of about 3,900 miles (6,300 km). Hummingbirds split from their sister group, the swifts and treeswifts, around 42 million years ago. The oldest known fossil hummingbird is Eurotrochilus, from the Rupelian Stage of Early Oligocene Europe. Page: Anna's hummingbird Summary: Anna's hummingbird (Calypte anna) is a North American species of hummingbird. It was named after Anna Masséna, Duchess of Rivoli. It is native to western coastal regions of North America. In the early 20th century, Anna's hummingbirds bred only in northern Baja California and Southern California. The transplanting of exotic ornamental plants in residential areas throughout the Pacific coast and inland deserts provided expanded nectar and nesting sites, allowing the species to expand its breeding range. Year-round residence of Anna's hummingbirds in the Pacific Northwest is an example of ecological release dependent on acclimation to colder winter temperatures, introduced plants, and human provision of nectar feeders during winter. These birds feed on nectar from flowers using a long extendable tongue. They also consume small insects and other arthropods caught in flight or gleaned from vegetation. Page: Ruby-throated hummingbird Summary: The ruby-throated hummingbird (Archilochus colubris) is a species of hummingbird that generally spends the winter in Central America, Mexico, and Florida, and migrates to Canada and other parts of Eastern North America for the summer to breed. It is the most common hummingbird in eastern North America, having population estimates of about 35 million in 2021. Invoking: `wikipedia` with `{'query': 'Fastest bird species'}` Page: List of birds by flight speed Summary: This is a list of the fastest flying birds in the world. A bird's velocity is necessarily variable; a hunting bird will reach much greater speeds while diving to catch prey than when flying horizontally. The bird that can achieve the greatest airspeed is the peregrine falcon (Falco peregrinus), able to exceed 320 km/h (200 mph) in its dives. A close relative of the common swift, the white-throated needletail (Hirundapus caudacutus), is commonly reported as the fastest bird in level flight with a reported top speed of 169 km/h (105 mph). This record remains unconfirmed as the measurement methods have never been published or verified. The record for the fastest confirmed level flight by a bird is 111.5 km/h (69.3 mph) held by the common swift. Page: Fastest animals Summary: This is a list of the fastest animals in the world, by types of animal. Page: Swift (bird) Summary: The swifts are a family, Apodidae, of highly aerial birds. They are superficially similar to swallows, but are not closely related to any passerine species. Swifts are placed in the order Apodiformes with hummingbirds. The treeswifts are closely related to the true swifts, but form a separate family, the Hemiprocnidae. Resemblances between swifts and swallows are due to convergent evolution, reflecting similar life styles based on catching insects in flight. The family name, Apodidae, is derived from the Greek ἄπους (ápous), meaning "footless", a reference to the small, weak legs of these most aerial of birds. The tradition of depicting swifts without feet continued into the Middle Ages, as seen in the heraldic martlet.- The scientific name for a hummingbird is Trochilidae. - The fastest bird species is the peregrine falcon (Falco peregrinus), which can exceed speeds of 320 km/h (200 mph) in its dives. > Finished chain. Total Tokens : 1579 Prompt Tokens : 1477 Completion Tokens : 102 Total Cost (USD) : $0.0008915000000000001 """ |
▶ 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
aiohttp==3.9.5 aiosignal==1.3.1 annotated-types==0.7.0 anyio==4.4.0 async-timeout==4.0.3 attrs==23.2.0 beautifulsoup4==4.12.3 certifi==2024.6.2 charset-normalizer==3.3.2 dataclasses-json==0.6.7 distro==1.9.0 exceptiongroup==1.2.1 frozenlist==1.4.1 greenlet==3.0.3 h11==0.14.0 httpcore==1.0.5 httpx==0.27.0 idna==3.7 jsonpatch==1.33 jsonpointer==3.0.0 langchain==0.2.5 langchain-community==0.2.5 langchain-core==0.2.9 langchain-openai==0.1.9 langchain-text-splitters==0.2.1 langsmith==0.1.82 marshmallow==3.21.3 multidict==6.0.5 mypy-extensions==1.0.0 numpy==1.26.4 openai==1.35.3 orjson==3.10.5 packaging==24.1 pydantic==2.7.4 pydantic_core==2.18.4 python-dotenv==1.0.1 PyYAML==6.0.1 regex==2024.5.15 requests==2.32.3 sniffio==1.3.1 soupsieve==2.5 SQLAlchemy==2.0.31 tenacity==8.4.2 tiktoken==0.7.0 tqdm==4.66.4 typing-inspect==0.9.0 typing_extensions==4.12.2 urllib3==2.2.2 wikipedia==1.4.0 yarl==1.9.4 |
※ pip install python-dotenv langchain-community langchain-openai wikipedia 명령을 실행했다.