使用GPT-4o-mini的LangChain搜索AI代理

2024年07月26日 由 alex 发表 190 0

简介

LangChain Agent 利用网络搜索来回答用户的问题。该代理可以访问一个工具,即 Tavily API,以搜索网络。


tools = [TavilySearchResults(max_results=1)]


考虑问题:What year was IBM founded and in what year was Apple founded?以及对问题的回答(如下所示),共使用了 165 个代币,价格为 0.0000594 美元。


其中 88 个提示代币的价格为 0.0000132 美元,77 个完成代币的价格为 0.0000462 美元。到第一个令牌的时间为 1389 毫秒,这一点很重要,因为为了获得更好的用户体验,可能会对响应进行流式处理,因此到第一个令牌的时间表示用户的等待时间。输出令牌的成本约为输入令牌的四倍。


基本设置

下面,我按照 Python 代码中的定义设置了 LangSmith 集成。langChain API 密钥可以从 LangSmith 控制台中获取,项目名称是给项目起的任意名称。


import os
from uuid import uuid4
unique_id = uuid4().hex[0:8]
os.environ[“LANGCHAIN_TRACING_V2”] = “true”
os.environ[“LANGCHAIN_PROJECT”] = f”OpenAI_SM_Agent_1"
os.environ[“LANGCHAIN_ENDPOINT”] = “https://api.smith.langchain.com"
os.environ[“LANGCHAIN_API_KEY”] = “<LangSmith API Key Goes Here>”


需要添加 OpenAI 和 Tavily 网络搜索 API 密钥...


os.environ[“OPENAI_API_KEY”] = getpass.getpass()
os.environ[“TAVILY_API_KEY”] = getpass.getpass()


模型设置为:


llm = ChatOpenAI(model=”gpt-4o-mini”)


LangSmith

在 LangSmith 中,可以访问 OpenAI_SM_Agent_1 项目,从中可以看到五个运行。选择第一个运行,可以看到链条中的每个步骤,以及每个步骤的成本和执行时间/延迟。


2


LangSmith 还可以创建数据集,对输出进行注释、设置正确与错误,并运行自动评估以确定正确性。


代理执行

代理将用户的复合问题分解为多个子问题,然后分别回答。


该代码演示了如何设置和使用 OpenAI 的语言模型(LLM),特别是 LangChain 框架。它还集成了 Tavily 搜索工具,以增强信息检索功能。


网络搜索 LangChain 代理:


### Install Necessary Packages
pip install -qU langchain-openai langchain langchain_community
### Import Required Modules
import getpass
import os
### Set Environment Variables for API Keys
os.environ["OPENAI_API_KEY"] = getpass.getpass()
os.environ["TAVILY_API_KEY"] = getpass.getpass()
### Initialize the OpenAI LLM
from langchain_openai import ChatOpenAI
###
llm = ChatOpenAI(model="gpt-4o-mini")
### Import Necessary LangChain Components
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_core.prompts import ChatPromptTemplate
### Set Up the Tavily Search Tool
tools = [TavilySearchResults(max_results=1)]
### Create a Chat Prompt Template
prompt = ChatPromptTemplate.from_messages(
    [
        (
            "system",
            "You are a helpful assistant. Make sure to use the tavily_search_results_json tool for information.",
        ),
        ("placeholder", "{chat_history}"),
        ("human", "{input}"),
        ("placeholder", "{agent_scratchpad}"),
    ]
)
### Construct the Tools agent
agent = create_tool_calling_agent(llm, tools, prompt)
### Create an agent executor by passing in the agent and tools
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
### Invoke the Agent Executor:
agent_executor.invoke({"input": "What year was IBM founded and in what year was Apple founded?"})


以及代理的输出结果。很明显,有两个查询,分别是 IBM 成立年份和苹果公司成立年份,最终输出结果与网络参考资料合并在一起。


> Entering new AgentExecutor chain...
Invoking: `tavily_search_results_json` with `{'query': 'year IBM founded'}`

[{'url': 'https://en.wikipedia.org/wiki/History_of_IBM', 'content': 'Recognizing this trend, management, with the support of the Board of Directors, began to implement a plan to split IBM into increasingly autonomous business units (e.g. processors, storage, software, services, printers, etc.) to compete more effectively with competitors that were more focused and nimble and had lower cost structures.[citation needed]\nIBM also began spinning off its many divisions into autonomous subsidiaries (so-called "Baby Blues") in an attempt to make the company more manageable and to streamline IBM by having other investors finance those companies.[180][181] These included AdStar, dedicated to disk drives and other data storage products (on creation the largest data storage business in the world);[182] IBM Application Business Systems, dedicated to mid-range computers; IBM Enterprise Systems, dedicated to mainframes; Pennant Systems, dedicated to mid-range and large printers; Lexmark, dedicated to small printers, keyboards, and typewriters (such as the Selectric); and more.[183] Lexmark was acquired by Clayton & Dubilier in a leveraged buyout shortly after its formation.[184]\nIn September 1992, IBM combined and spun off their various non-mainframe and non-midrange, personal computer manufacturing divisions into an autonomous wholly owned subsidiary known as the IBM Personal Computer Company (IBM PC Co.).[185][186] Purchases were often instigated by middle managers and senior staff who saw the potential – once the revolutionary VisiCalc spreadsheet, the killer app, had been surpassed by a far more powerful and stable product, Lotus 1-2-3.[citation needed]\nIBM\'s dominance of the mainframe market in Europe and the US encouraged existing customers to buy the PC,[171][173] and vice versa; as sales of what had been an experiment in a new market became a substantial part of IBM\'s financials, the company found that customers also bought larger IBM computers.[174][167][162] Unlike the BUNCH and other rivals IBM quickly adjusted to the retail market,[171][175] with its own sales force competing with outside retailers for the first time.[162] The US government\'s case sustained by four US Presidents and their Attorneys General was dropped as "without merit" in 1982 by William Baxter, US President Reagans\' Assistant Attorney General in charge of the Antitrust Division of the U.S. Department of Justice.[315]\nCDC filed an antitrust lawsuit against IBM in Minnesota\'s federal court alleging that IBM had monopolized the market for computers in violation of section 2 of the Sherman Antitrust Act by among other things announcing products it could not deliver.[316] A 1965 internal IBM memo by an IBM attorney noted that Control Data had publicly blamed its declining earnings on IBM, "and its frequent model and price changes. By 1981 its stock price had declined by 22%.[166] IBM\'s earnings for the first half of the year grew by 5.3% – one third of the inflation rate – while those of DEC grew by more than 35%.[165] Although IBM began selling minicomputers,[170] in January 1982 the Justice Department ended the antitrust suit because, The New York Times reported, the government "recognized what computer experts and securities analysts had long since concluded: I.B.M. no longer dominates the computer business".[147]\nIBM wished to avoid the same outcome with the new personal computer industry.[169] He asserts that the company was not plundered, its leased machinery was not confiscated, and IBM continued to receive funds through its Geneva-based subsidiary.[86] Black argues that IBM persisted in its business relations with the Nazi regime beyond the point where they should have ceased, maintaining and expanding services to the Third Reich[86] until the seizure of Dehomag following the United States\' declaration of war against Germany in 1941.[citation needed]\nIBM countered these claims by stating that the allegations were based on known facts and previously disclosed documents, asserting the absence of new revelations.'}]
Invoking: `tavily_search_results_json` with `{'query': 'year Apple founded'}`

[{'url': 'https://en.wikipedia.org/wiki/History_of_Apple_Inc.', 'content': 'The head of a retail chain said "It appears that IBM had a better understanding of why the Apple II was successful than had Apple".[78] Gene Amdahl predicted that Apple would be another of the many "brash young companies" that IBM had defeated.[88]\nBy 1984 the press called the two companies archrivals,[89] but IBM had $4 billion in annual PC revenue, more than twice that of Apple and as much as the sales of it and the next three companies combined.[90] A Fortune survey found that 56% of American companies with personal computers used IBM PCs, compared to 16% for Apple.[91] Small businesses, schools, and some homes became the II\'s primary market.[76]\nXerox PARC and the Lisa[edit]\nApple Computer\'s business division was focused on the Apple III, another iteration of the text-based computer. In 1979, the Apple II was chosen to be the desktop platform for the first "killer application" of the business world: VisiCalc, a spreadsheet.[55] So important that the Apple II became what John Markoff described as a "VisiCalc accessory",[57] the application created a business market for the computer and gave home users an additional reason to buy it: compatibility with the office.[55] Before VisiCalc, Apple had been a distant third place competitor to Commodore and Tandy.[58][59]\nThe Apple II was one of the three "1977 Trinity" computers generally credited with creating the home computer market (the other two being the Commodore PET and the Tandy Corporation TRS-80).[60] After giving their results for the first quarter of 2011, Microsoft\'s net profits of $5.2 billion were lower for the quarter than those of Apple, which earned $6 billion in net profit for the quarter.[182][183] The late April announcement of profits by the companies marked the first time in 20 years that Microsoft\'s profits had been lower than Apple\'s,[184] a situation described by Ars Technica as "unimaginable a decade ago".[182]\nThe Guardian reported that one of the reasons for the change was because PC software, where Microsoft dominates, has become less important compared to the tablet and smartphone markets, where Apple has a strong presence.[184] One reason for this was a surprise drop in PC sales in the quarter.[184] Nonetheless, he kept his word and paid the two Steves the money promised.[39][37][38][40]\nThe Apple I went on sale in July 1976 as an assembled circuit board with a retail price of $666.66.[41][42][43] Wozniak later said he had had no idea about the relation between the number and the mark of the beast, and that he came up with the price because he liked repeating digits.[39] About 200 units of the Apple I were eventually sold.[44]\nThe Apple I computer had some notable features, including the use of a TV display, whereas many machines had no display at all. The new corporation bought out the partnership the two Steves had formed nine months earlier.[46]\nIn February 1977, Markkula recruited Michael Scott from National Semiconductor to serve as the first president and CEO of Apple Computer, as the two Steves were both insufficiently experienced and he was not interested in taking that position himself.[47][48]\nThat same month, Wozniak resigned from his job at Hewlett-Packard to work full-time for Apple.[46][49]\nApple II[edit]\nAlmost as soon as Apple had started selling its first computers, Wozniak moved on from the Apple I and began designing a greatly improved computer: the Apple II.[45] Wozniak completed a working prototype of the new machine by August 1976.[38][50]'}]IBM was founded in 1911, originally as the Computing-Tabulating-Recording Company (CTR), and was renamed International Business Machines Corporation (IBM) in 1924. 
Apple Inc. was founded in 1976. 
For more detailed histories, you can visit the following links: 
- [IBM History](https://en.wikipedia.org/wiki/History_of_IBM)
- [Apple History](https://en.wikipedia.org/wiki/History_of_Apple_Inc.)
> Finished chain.
{'input': 'What year was IBM founded and in what year was Apple founded?',
 'output': 'IBM was founded in 1911, originally as the Computing-Tabulating-Recording Company (CTR), and was renamed International Business Machines Corporation (IBM) in 1924. \n\nApple Inc. was founded in 1976. \n\nFor more detailed histories, you can visit the following links: \n- [IBM History](https://en.wikipedia.org/wiki/History_of_IBM)\n- [Apple History](https://en.wikipedia.org/wiki/History_of_Apple_Inc.)'}


下面只是代理的输入和输出文本。


{'input': 'What year was IBM founded and in what year was Apple founded?',
 'output': 'IBM was founded in 1911, originally as the Computing-Tabulating-Recording Company (CTR), and was renamed International Business Machines Corporation (IBM) in 1924. \n\nApple Inc. was founded in 1976. \n\nFor more detailed histories, you can visit the following links: \n- [IBM History](https://en.wikipedia.org/wiki/History_of_IBM)\n- [Apple History](https://en.wikipedia.org/wiki/History_of_Apple_Inc.)'}


总之,这段代码利用 LangChain 框架建立了一个基于 LLM 的代理,集成了一个自定义搜索工具,并用它来回答一个特定的查询。


总结


GPT-4o-mini 注意事项

  • 本地控制: 开源 SLM 具有在本地运行模型并完全控制推理的优势,这不适用于 OpenAI 的商业托管 API 模型。
  • OpenAI 的重点: OpenAI 强调速度、成本和能力,同时紧跟模型小型化的趋势。
  • 竞争对手: Orca-2、Phi3 和 TinyLlama 等基于文本的开源 SLM 能力很强,是值得注意的竞争对手。
  • 与众不同之处: GPT-4o-mini 以其成本、速度、能力和可用模式脱颖而出。


GPT-4o-mini 的优势

  • 文本和视觉支持: GPT-4o-mini 的应用程序接口(API)和游戏场都支持文本和视觉输入。
  • 未来扩展: 即将推出的功能包括处理文本、图像、视频和音频输入和输出。
  • 大型上下文窗口: 该机型拥有 128K 代币的上下文窗口,知识储备可持续到 2023 年 10 月。
  • 多语言功能: 该模型支持多种语言。
  • 推理速度更快: 更快的推理速度使其适用于各种应用。
  • 成本效益高: 速度与成本的完美结合使其成为具有多个并行调用的代理应用程序的理想选择。它的成本为每百万输入词组 15 美分,每百万输出词组 60 美分。
  • 微调:GPT-4o-mini 的微调功能即将推出。
文章来源:https://medium.com/@cobusgreyling/langchain-search-ai-agent-using-gpt-4o-mini-ce2b7442479b
欢迎关注ATYUN官方公众号
商务合作及内容投稿请联系邮箱:bd@atyun.com
评论 登录
写评论取消
回复取消