본문 바로가기
📚도서 공부/LangChain으로 구현하는 LLM

4-3-1(툴을 사용한 질문 응답) Langchain 비서 구축

by Majestyblue 2024. 4. 23.

3. 툴을 사용한 질문 응답

1. 툴을 사용한 정보 검색

Langchain에서는 다양한 도구를 사용할 수 있다. 아래 코드는 LangChain 라이브러리를 이용하여 특정 질문에 대한 답변을 생성하는데 이 과정에서 Duck Duck go 검색, Arxiv, Wikipedia을 활용하는 예제이다.

%%time
# <https://github.com/langchain-ai/langchain/issues/7747>
from langchain.agents import AgentExecutor, AgentType, initialize_agent, load_tools
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler

#위에서 선언하였음.
# from langchain.llms import GPT4All
# model = "E:\\GPT4ALL\\Download Models\\mistral-7b-instruct-v0.2.Q4_0.gguf"
# llm = GPT4All(model=model, device='gpu', streaming=True)

def load_agent(llm_model) -> AgentExecutor:
    # DuckDuckGoSearchRun, wolfram alpha, arxiv search, wikipedia
    # TODO: try wolfram-alpha!
    tools = load_tools(
        tool_names=["ddg-search", "arxiv", "wikipedia"],
        llm=llm
    )
    return initialize_agent(
        tools=tools, llm=llm_model, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)

agent = load_agent(llm)
agent.run("What is the difference between type 1 diabetes and type 2 diabetes?")
  • from langchain.agents import AgentExecutor, AgentType, initialize_agent, load_tools: langchain.agents 모듈에서 AgentExecutor, AgentType, initialize_agent, load_tools 함수를 임포트한다. 이들은 에이전트를 초기화하고 실행하는 데 필요한 함수와 클래스이다.
  • from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler: 스트리밍 출력을 처리하기 위한 StreamingStdOutCallbackHandler 클래스를 langchain.callbacks.streaming_stdout 모듈에서 임포트하다.
  • # llm = GPT4All(model=model, device='gpu', streaming=True): 주석 처리된 코드로, GPU를 사용하여 스트리밍 모드에서 모델을 로드한다. 실제 실행에는 포함되지 않습니다. streaming 변수는 모든 텍스트가 완료될 때가 아니라 텍스트 응답이 도착할 때마다 업데이트 된다. (OpenAI, ChatOpenAI, ChatAnthropic 등 구현 가능, GPT4All에서도 당연히 가능)
  • def load_agent(llm_model) -> AgentExecutor:: llm_model을 인자로 받아 AgentExecutor 객체(체인)를 반환하는 load_agent 함수를 정의한다. 이 함수는 특정 LLM 모델을 사용하여 에이전트를 로드하는 작업을 담당한다.
  • tools = load_tools(tool_names=["ddg-search", "arxiv", "wikipedia"], llm=llm): "ddg-search", "arxiv", "wikipedia" 도구를 로드하여 tools 변수에 저장한다. 이 도구들은 에이전트가 정보를 수집하는 데 사용된다.
  • return initialize_agent(tools=tools, llm=llm_model, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True): 초기화된 에이전트를 반환한다. AgentType.ZERO_SHOT_REACT_DESCRIPTION는 에이전트의 유형을 지정하며, verbose=True는 상세 로그를 출력하도록 설정한다.
  • agent = load_agent(llm): load_agent 함수를 호출하여 에이전트를 로드하고 agent 변수에 할당한다.
  • agent.run("What is the difference between type 1 diabetes and type 2 diabetes?"): 로드된 에이전트를 사용하여 "type 1 diabetes와 type 2 diabetes의 차이점은 무엇인가?"라는 질문에 대한 답변을 생성한다.

<wikipedia>

> Entering new AgentExecutor chain...
 I need to find information about both types of diabetes.
Action: wikipedia
Action Input: Type 1 diabetes, Type 2 diabetes
Observation: Page: Type 3 diabetes
Summary: Type 3 diabetes is a proposed pathological linkage between Alzheimer's disease and certain features of type 1 and type 2 diabetes.  Specifically, the term refers to a set of common biochemical and metabolic features seen in the brain in Alzheimer's disease, and in other tissues in diabetes; it may thus be considered a "brain-specific type of diabetes."  It was recognized at least as early as 2005 that some features of brain function in Alzheimer's disease mimic those that underlie diabetes.  However, the concept of type 3 diabetes is controversial, and as of 2021 it was not an officially recognized diagnosis.
Metabolic risk factors such as hyperglycaemia,  oxidative stress and lipid peroxidation are common processes thought to be contributors to the development of Alzheimer's disease in people with diabetes.  But while insulin resistance is a risk factor for the development of Alzheimer's disease and some other dementias, causes of Alzheimer's disease are likely to be much more complex than being explained by insulin factors on their own, and indeed several patients with Alzheimer's disease have normal insulin metabolism.
The techniques used to prevent the disease in patients with diabetes are similar to individuals who do not show signs and symptoms of the disease. The four pillars of Alzheimer's disease prevention are currently used as a guide for individuals of who are at risk of developing Alzheimer's disease.  As with Alzheimer's disease more broadly, there is no cure for type 3 diabetes, but disease progression may be slowed with certain drugs.

Page: Diabetes
Summary: Diabetes mellitus, often known simply as diabetes, is a group of common endocrine diseases characterized by sustained high blood sugar levels. Diabetes is due to either the pancreas not producing enough insulin, or the cells of the body becoming unresponsive to the hormone's effects. Classic symptoms include thirst, polyuria, weight loss, and blurred vision. If left untreated, the disease can lead to various health complications, including disorders of the cardiovascular system, eye, kidney, and nerves. Untreated or poorly treated diabetes accounts for approximately 1.5 million deaths every year.
The major types of diabetes are type 1 and type 2, though other forms also exist. The most common treatment for type 1  is insulin replacement therapy (insulin injections), while anti-diabetic medications (such as metformin and semaglutide) and lifestyle modifications can be used to manage type 2. Gestational diabetes, a form that arises during pregnancy in some women, normally resolves shortly after delivery.
As of 2021, an estimated 537 million people had diabetes worldwide accounting for 10.5% of the adult population, with type 2 making up about 90% of all cases. It is estimated that by 2045, approximately 783 million adults, or 1 in 8, will be living with diabetes, representing a 46% increase from the current figures. The prevalence of the disease continues to increase, most dramatically in low- and middle-income nations. Rates are similar in women and men, with diabetes being the seventh leading cause of death globally. The global expenditure on diabetes-related healthcare is an estimated US$760 billion a year.

Page: Type 1 diabetes
Summary: Type 1 diabetes (T1D), formerly known as juvenile diabetes, is an autoimmune disease that originates when cells that make insulin (beta cells) are destroyed by the immune system. Insulin is a hormone required for the cells to use blood sugar for energy and it helps regulate glucose levels in the bloodstream. Before treatment this results in high blood sugar levels in the body. The common symptoms of this elevated blood sugar are frequent urination, increased thirst, increased hunger, weight loss, and other serious complications. Additional symptoms may include blurry vision, tiredness, and slow wound healing. Symptoms typically develop over a short period of time, often a matter of weeks if

 

 

<duckduckgo_search>

Thought: I need to find more specific information about the differences between Type 1 and Type 2 diabetes.
Action: duckduckgo_search
Action Input: type 1 diabetes vs type 2 diabetes
Observation: Learn how Type 1 and Type 2 diabetes differ in causes, symptoms, treatment and complications. Find out how to manage your life with diabetes and prevent or reverse some of the effects. Diabetes is a chronic condition that affects how your body turns food into energy. Learn about the three main types of diabetes: type 1, type 2, and gestational diabetes, and how they differ in symptoms, causes, and treatment. Learn the differences between type 1 and type 2 diabetes, two chronic conditions that affect blood glucose regulation in the body. Find out how they are diagnosed, treated, and how to prevent complications. Learn how type 1 and type 2 diabetes differ in how they develop, affect insulin and the pancreas, and are diagnosed and treated. Find out who is most affected, the role of risk factors, and the age of diagnosis for each type of diabetes. Type 1 and type 2 diabetes are both diseases of high blood sugar, but with different origin stories and treatments. Type 1 diabetes is an autoimmune disease that requires insulin therapy for life ...
Thought:
E:\\miniconda3\\envs\\gpt4all_p\\lib\\site-packages\\curl_cffi\\aio.py:204: UserWarning: Curlm alread closed! quitting from process_data
  warnings.warn("Curlm alread closed! quitting from process_data")
 Based on my observations, I now know that the main differences between Type 1 and Type 2 diabetes lie in their causes, symptoms, treatment, and complications.
Final Answer: The primary difference between Type 1 and Type 2 diabetes lies in their underlying causes - Type 1 is an autoimmune disease where the body's immune system attacks and destroys insulin-producing cells (beta cells) in the pancreas, while Type 2 is a metabolic disorder characterized by high blood sugar levels due to insulin resistance or deficiency. Symptoms, treatment methods, and complications also differ between the two types.

 

 

<arxiv인가?>

Thought:
E:\\miniconda3\\envs\\gpt4all_p\\lib\\site-packages\\curl_cffi\\aio.py:204: UserWarning: Curlm alread closed! quitting from process_data
  warnings.warn("Curlm alread closed! quitting from process_data")
 Based on my observations, I now know that the main differences between Type 1 and Type 2 diabetes lie in their causes, symptoms, treatment, and complications.
Final Answer: The primary difference between Type 1 and Type 2 diabetes lies in their underlying causes - Type 1 is an autoimmune disease where the body's immune system attacks and destroys insulin-producing cells (beta cells) in the pancreas, while Type 2 is a metabolic disorder characterized by high blood sugar levels due to insulin resistance or deficiency. Symptoms, treatment methods, and complications also differ between the two types.

 

 

<이건 LLM 자체인듯>

> Finished chain.
CPU times: total: 5.72 s
Wall time: 2min 1s
"The primary difference between Type 1 and Type 2 diabetes lies in their underlying causes - Type 1 is an autoimmune disease where the body's immune system attacks and destroys insulin-producing cells (beta cells) in the pancreas, while Type 2 is a metabolic disorder characterized by high blood sugar levels due to insulin resistance or deficiency. Symptoms, treatment methods, and complications also differ between the two types."
  • DuckDuckGo: 개인정보에 중점을 둔 검색 엔진, 개발자 등록이 필요하지 않는 것이 이점이다.
  • Wolfram Alpha: 자연어 이해와 수학 능력을 결합한 통합 도구로 “2x+5 = -3x+7”과 같은 질문에 대한 답변을 제공한다. 이를 사용하려면 계정을 설정하고 웹사이트에서 생성한 개발자 토큰으로 WOLFRAM_ALPHA_APPID 환경변수를 설정해야 한다.
  • arXiv: 학술 전자 출팜물 검색, 연구 중심의 질문에 유용하다.
  • Wikipedia: 유명한 엔티티에 관한 모든 지문에 대해 사용된다.