[PYTHON/LANGCHAIN] create_react_agent 함수 : compiledStateGraph 객체 만들기
■ create_react_agent 함수를 사용해 compiledStateGraph 객체를 만드는 방법을 보여준다. ※ 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 |
from dotenv import load_dotenv from langchain_core.documents import Document from langchain_openai import OpenAIEmbeddings from langchain_core.vectorstores import InMemoryVectorStore from langchain_openai import ChatOpenAI from langgraph.prebuilt import create_react_agent load_dotenv() documentList = [ Document(page_content = "Dogs are great companions, known for their loyalty and friendliness."), Document(page_content = "Cats are independent pets that often enjoy their own space." ) ] openAIEmbeddings = OpenAIEmbeddings() inMemoryVectorStore = InMemoryVectorStore.from_documents( documentList, embedding = openAIEmbeddings ) vectorStoreRetriever = inMemoryVectorStore.as_retriever( search_type = "similarity", search_kwargs = {"k" : 1} ) chatOpenAI = ChatOpenAI(model = "gpt-4o-mini") tool = vectorStoreRetriever.as_tool( name = "pet_info_retriever", description = "Get information about pets.", ) toolList = [tool] compiledStateGraph = create_react_agent(chatOpenAI, toolList) for addableUpdatesDict in compiledStateGraph.stream({"messages" : [("human", "What are dogs known for?")]}): print(addableUpdatesDict) print("-" * 100) """ { 'agent' : { 'messages' : [ AIMessage( content = 'Dogs are known for several characteristics and traits, including:\n\n1. **Companionship**: Dogs are often referred to as "man\'s best friend" due to their loyalty and companionship.\n\n2. **Intelligence**: Many dog breeds are highly intelligent and capable of learning a variety of commands and tricks.\n\n3. **Variety of Breeds**: There are hundreds of dog breeds, each with its own unique traits, sizes, and temperaments.\n\n4. **Working Abilities**: Dogs are used in various roles, such as service animals, search and rescue, therapy dogs, and police or military dogs.\n\n5. **Strong Sense of Smell**: Dogs have an exceptional sense of smell, which makes them excellent for tracking and detection purposes.\n\n6. **Social Behavior**: Dogs are social animals and often thrive in the company of humans and other pets.\n\n7. **Playfulness**: Many dogs enjoy playing and being active, which makes them great companions for outdoor activities.\n\n8. **Emotional Support**: Dogs are known to provide emotional support and comfort to their owners, often sensing when someone is feeling down.\n\n9. **Protectiveness**: Many dogs have a natural instinct to protect their home and family, making them good guard animals.\n\n10. **Communication**: Dogs communicate through a combination of vocalizations, body language, and facial expressions. \n\nOverall, dogs are appreciated for their loyalty, intelligence, and the deep bond they can form with humans.', additional_kwargs = {'refusal' : None}, response_metadata = { 'token_usage' : { 'completion_tokens' : 299, 'prompt_tokens' : 58, 'total_tokens' : 357, 'completion_tokens_details' : {'reasoning_tokens' : 0} }, 'model_name' : 'gpt-4o-mini-2024-07-18', 'system_fingerprint' : 'fp_f85bea6784', 'finish_reason' : 'stop', 'logprobs' : None }, id = 'run-b2d78792-6c54-422e-8739-07662d2eb56b-0', usage_metadata = {'input_tokens' : 58, 'output_tokens' : 299, 'total_tokens' : 357} ) ] } } |
""" —————————————————————————————————-