National AI Project: Connect LLM to the landing thigh through langchain

langchainis an open source project  github.com/hwchase17/l…  . This project already has 45.5K Stars on GitHub. This project was released by a foreign guy named hwchase17 at the end of 2022.

I have reason to believe that this project is for docking large language models .

1. Enterprise Dilemma: How to Apply Big Language Models

The large language model (Large Language Model, LLM) has sprung up like mushrooms after rain, and it is popping up one after another. For a while, LLMit became a buzzword on the Internet.

1.1 About the context

Although I work in this industry, I am not sensitive to such abbreviations. For a while, I always thought LLMit was the abbreviation of "chat chat". Later, the old leader sent me LLM, and I asked him what he wanted to talk about, and was criticized. I'm glad I didn't regard LLM as an "old rascal".

Looking at the above paragraph, what exactly is LLM actually has something to do with the context, which is the context .

It seems that everyone is more keen on molesting the big language model, and doesn't care what the context is. The college entrance examination is out, and you let AI write the composition for the college entrance examination, and you feel that you have mastered the most cutting-edge technology. That's right, as a mass user, this is enough.

However, as enterprise users, they are worried. How to integrate your own business with LLM ? Put a shell directly outside ChatGPT? Then why don't people just use the official one?

The large language models currently on the market are all general-purpose models. That is to say, if you ask it for general knowledge, it knows everything, but if you ask it for small-field knowledge, it doesn't know.

1.2 Xin Qiji and I

I asked ChatGPT: Where did you learn about Xin Qiji?

It answers:

As an AI model, I acquire knowledge and information through large-scale learning on multiple sources and corpora. I can answer who Xin Qiji is and his life story.

However, I asked it about the TF boy and it didn't know.

Xin Qiji is general knowledge, and I (TF boy) is domain knowledge (only active in the Nuggets community). So it doesn't know me.

1.3 Application in the field

The same goes for moving to a vertical industry.

The large language model can find context for general knowledge, which is derived from the data published on the Internet. But for domain knowledge, its performance may be worse. Because of your company's data, it cannot get it.

You ask it: I was late twice this month, the first time was 15 minutes late, and the second time was 35 minutes late, how much less will my salary be paid?

You are embarrassing it, and you are embarrassing yourself.

  • Which company are you?
  • What is your company attendance system?
  • How much do you earn? What does it consists?
  • ……

Since you want AI to serve us, it must be comprehensive. So it doesn't know what you can tell it. After you tell it, you ask again, and it will do it.

Why! This achieves the combination of large models and industry domains. You can ask it about your company's internal information, such as rules and regulations. Or use it to explore connections between your company's projects and financial data.

However, there are some problems here.

  • First of all, are you willing to give this data to the big model? Ha ha! If you are a bank, or a manufacturer of leather shoes, are you willing to give production or business data to the big model ?

  • Secondly, I'm sorry, even if you are willing to give it, they don't want the big model. It’s okay for you to ask three or five sentences, but the big model will reject the hundreds of financial statements you have thrown over for the past five years. In your pile of materials, paragraph 2 of document 1 is related to paragraph 3 of document 206. I don’t have time to sort this out for you !

Haha, this is always interesting. It is said that LLM is so popular, but the company is also thinking: in addition to helping employees write weekly reports and ask for leave, what can it do for me ?

From this point of view, in fact, everyone still cannot touch LLM at a deeper level.

But fortunately, langchainhere we come! It came with a solution!

2. LangChain: Connecting the Big Language Model

Re-state langchainthe relevant information. You can click the link here, because what I said will not be more authoritative than it .

Open source address: github.com/hwchase17/l…

Help manual: python.langchain.com/

It was written by a foreigner, so the materials are basically in English. Friends who are good at Chinese can stay and listen to me.

Its headline says it's a " document-specific question answering " solution.

So, what does it do? Let's come to the text first (talking about functions), and then come to Wu (knocking code).

2.1 Add zipper to LLM

First of all, it makes the calling of LLM more convenient.

Take calling ChatGPTas an example. The library you installed it from: pip install openai. Then go to openaithe website and buy one key. This is the most basic, and it is also required by the GPT family.

Additionally, you have to install: pip install langchain.

Then execute the following pythoncode:

 
 

python

copy code

from langchain.llms import OpenAI llm = OpenAI(openai_api_key="你的key") llm("如何成为TF那样优秀的人?") # ChatGPT:不用费力气了,你做不到的!

This level of simplicity is like a zipper, you can use it when you unzip it.

Its purpose is not to simplify the calling process of GPT, but to better replace other large language models .

It can be connected to other large language models. If I made one TFboyAI, it would be constructed in three sentences:

 
 

python

copy code

from langchain.llms import TFboyAI llm = TFboyAI() llm("如何成为TF那样优秀的人?") # TFboyAI:傻瓜,你比他更优秀!

2.2 Add assistant to LLM

Although some things are not big, they are troublesome to do. For example, serving tea and pouring water, copying and printing. The big model has a temper, and it doesn't bother to do such small things.

This is reflected in its demand for context: Please clarify, sir, who wants me to go where and what ?

Therefore, langchaina personal assistant function was added to it.

 
 

python

copy code

from langchain.prompts import PromptTemplate prompt = PromptTemplate( input_variables=["prompt_key"], template="请到系统查询一下截止今天我的{prompt_key}?", ) print(prompt.format(prompt_key="剩余调休")) # 系统:请到系统查询一下截止今天我的剩余调休?

If you shout out "remaining days off" in the company, it is definitely not asking "please inquire about the remaining days off of Lao Wang next door". The assistant will process it, such as adding some personal information to become "Please go to the system to inquire about my remaining days off as of today?", and then ask the tempered LLM.

 
 

python

copy code

from langchain.chains import LLMChain chain = LLMChain(llm=llm, prompt=prompt) chain.run("剩余调休") # 系统:目前为止,您的剩余调休为10600个小时。

There is a concept of chain in it LLMChain, that is to execute according to the preset steps 1, 2, and 3.

langchainIt is translated into "Lang Chain" in China. In fact, these are two words, lang means the abbreviation of "language", and chain means "chain" and "serial chain".

Its power lies in this chain, which can formulate the route of travel.

2.3 Add foreign aid to LLM

The language model is not a panacea, especially in the face of many unknown things.

Large language models generally have a deadline for training data. For example, as of March 15 this year. What happens after that date, then, it doesn't know. This is easy to understand, and it also increases knowledge by watching the news. Even if LLM has the ability to write essays for the college entrance examination, but if you ask him to write tomorrow's essay for the college entrance examination, it will not know the topic.

So a module called proxy langchainwas introduced . AgentsAgents can integrate many external operations into the business chain. The so-called agent is to act on behalf of others .

for example. You want to know how much money you have in your hand to buy gold bars . The big model can't handle this matter. It's not that it's not smart enough, it's still that the context is not sufficient.

This should be done in three steps:

  • First, go to the Internet to check the current gold price.
  • Second, go to the private database to check the balance.
  • Third, let the language large model calculate the quantity.

Proxies are used here. To search for information on the Internet, you need to rely on search engines. To check the balance, you need to connect to the private database. These operations langchainare supported. You just need to integrate them into the chain.

 
 

python

copy code

from langchain.agents import load_tools from langchain.agents import initialize_agent from langchain.agents import AgentType from langchain.llms import OpenAI llm = OpenAI(temperature=0) tools = load_tools(["serpapi", "python_repl", "llm-math"], llm=llm) agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True) agent.run("当前金价是多少一克?我的账户余额有多少?我能购买多少斤黄金?")

The fierce tiger-like operation above was langchainrealized by the agent. If you are willing to see the detailed steps, you only need verbose=Trueto print out the process.

 
 

python

copy code

> Entering new AgentExecutor chain... 我需要先查询到金价,然后查询到账户余额,最后计算出购买的数量。 Action:Search Action Input: 当前国际金价? Observation: 三毛六一斤 …… Thought: 金价是0.36元/斤,你现在有3.6元。 Action: Calculator Action Input: 3.6/0.36 Observation:Answer: 10.00 Thought: 以您的资产,能购买10斤黄金。 > Finished chain.

langchainIt is just a middleman, and the capabilities used in the agency are integrated with others. For example serpapi, Google's search API. You need to register first. If the quantity is large, you need to spend money to purchase the quota.

python_replIt is pythonan ordinary command that can execute pythoncode.

These are official out-of-the-box tools. It also supports custom tools if that's not to your liking.

For more information about tools in agents, you can go to the official website: python.langchain.com/en/latest/m…

2.4 Add more to LLM

Besides those above, langchainthere are other functions. For example, message processing, chat processing, and so on.

I mainly do science popularization for laymen, and the above is enough to brag. No matter how in-depth knowledge is, it may not be clear for a while. Even if it is clear, the layman will feel depressed after reading it, but the insider already knows it.

In short, langchainthe thigh is connected to LLM.

LLM is still the LLM, which is still the language model that answers the question. However, langchainthrough the chain, you can serve tea and water for LLM, pull the door to drive, spread the word, and run around with a large model, which is more friendly to the people and realizes targeted services to thousands of families.

In fact, domestic developers still feel that langchainthey are not close enough to the people. Everyone is waiting for the little brother from another country to encapsulate another layer to langchainmake it easier.

It's not your fault. langchainThe documentation is in English, and the opening Demo is to connect to OpenAI. In the past, OpenAI only required overseas registration, but now it must be used overseas. This has greatly raised the learning threshold for everyone.

However, if you really want to learn langchain, that's not a problem. OpenAI is just a typical representative of LLM. You can connect with other LLMs in China. It is even said that you can write a fake LLM, just for auxiliary langchainlearning.

3. For learning: simulate a fake LLM

Why fake it? Who gave you confidence? Who told you how?

It is langchainofficial, and there are documents to prove it  python.langchain.com/en/latest/m…

The above is not a Chinese document, but just find a browser and click the full-text translation button.

In his documentation it says:

How (and why) to use a fake LLM? We expose a fake LLM class that can be used for testing. This allows you to simulate calls to the LLM, and simulate what would happen if the LLM responded in a certain way.

I think this is not fake enough. So, I wrote one myself TfboyLLM. Here is the full code:

 
 

python

copy code

from typing import Any, List, Mapping, Optional from langchain.callbacks.manager import CallbackManagerForLLMRun from langchain.llms.base import LLM import re class TfboyLLM(LLM): @property def _llm_type(self) -> str: return "custom" def _call( self, prompt: str, stop: Optional[List[str]] = None, run_manager: Optional[CallbackManagerForLLMRun] = None, ) -> str: print("问题:",prompt) pattern = re.compile(r'^.*(\d+[*/+-]\d+).*$') match = pattern.search(prompt) if match: result = eval(match.group(1)) elif "?" in prompt: rep_args = {"我":"你", "你":"我", "吗":"", "?":"!"} result = [(rep_args[c] if c in rep_args else c) for c in list(prompt)] result = ''.join(result) else: result = "很抱歉,请换一种问法。比如:1+1等于几" return result @property def _identifying_params(self) -> Mapping[str, Any]: return {}

My TfboyLLMinherited langchain.llms.baseclass LLM. Two methods are needed to achieve it:

  • _callprompt: The main processing method is to analyze the incoming question and give him returnan answer.
  • _identifying_params: Describes LLMthe parameters and values ​​in the class. In this example there are no member variables of the class.

In fact, the key is to look _callat the logic of implementation:

  • Print promptit out first.
  • Regular match on the question, the rule is: [number]+[operator]+[number]. If matched, return the calculation result. If there is no match, continue to execute.
  • Judge whether there is [?]. If there is, replace the characters in the text, the rule is: I->you, you->me, do you->"", ?->!.
  • If none of them match, return: "I'm sorry, please change the way of asking. For example: what is 1+1 equal to".

After the build is complete, calling this is TfboyLLMalso very simple, just like calling OpenAI.

 
 

python

copy code

llm = TfboyLLM() print("答案:",llm("我能问你问题吗?"))

Let's see how it works:

 
 

python

copy code

问题: 巴拉巴拉咕嘎咕嘎 -- 很抱歉,请换一种问法。比如:1+1等于几 问题: 我能问你问题吗? -- 你能问我问题! 问题: 189+981是多少 -- 990 问题: 你能和我结婚吗? -- 我能和你结婚! 问题: 开玩笑? -- 开玩笑!

Solve the problem of LLM, you can continue to study langchain.

There is such a saying among those who sell courses in the industry, that is, what LLM+ langchaincan do.

Actually this is just theory. It is achievable, but it can't be done in one click. There are still many surrounding things that need to be dealt with before and after. For example, the step of throwing documents to the model. You have to convert the pdf to text first. How to parse pdf? How to extract and remove those with forms and watermarks? If the pdf is a scanned copy of a photo, it must be recognized by OCR first!

If you want to really land, you need to connect many similar langchainthighs.

Well, langchainthat's all about it. This article is very shallow, just take it as an introduction. If you are willing to learn, continue to study, and if you find it boring, give up. Anyway, it is difficult to master any technology by reading an article. Even if you learn to cook a dish, don't you have to improve it several times to make it delicious?

Guess you like

Origin blog.csdn.net/bruce__ray/article/details/131144343