langchain-简单DEMO案例

1.langchain简单运用

1.1.基础配置

1
!pip install langchain
1
import langchain
1
!pip install openai
Successfully installed openai-0.28.0
1
2
import os
os.environ["OPENAI_API_KEY"] = "sk-OEHX9NSE2GUYSsene6SyT3BlbkFJMFsuXXXXXXXXX"
1
from langchain.llms import OpenAI
1
llm = OpenAI(temperature=0.9)
1
2
text = "What are 5 vacation destinations for someone who likes to eat pasta?"
print(llm(text))


​ 1. Rome, Italy
​ 2. Venice, Italy
​ 3. Bologna, Italy
​ 4. Palermo, Sicily
​ 5. Naples, Italy

1
from langchain.prompts import PromptTemplate
1
2
3
4
prompt = PromptTemplate(
input_variables=["food"],
template="What are 5 vacation destinations for someone who likes to eat {food}?"
)
1
print(prompt.format(food="sushi"))
What are 5 vacation destinations for someone who likes to eat sushi?
1
print(llm(prompt.format(food="sushi")))
1. Tokyo, Japan 
2. Osaka, Japan 
3. Sapporo, Japan 
4. Koh Samui, Thailand 
5. Hong Kong, China

1.2. Chains:Combine LLMs and prompts in multi-step workflows

1
2
3
from langchain.prompts import PromptTemplate
from langchain.llms import OpenAI
from langchain.chains import LLMChain
1
2
3
4
5
llm = OpenAI(temperature=0.9)
prompt = PromptTemplate(
input_variables=["food"],
template = "What are 5 vacation destinations for someone who likes to eat {food}?"
)
1
chain = LLMChain(llm=llm, prompt=prompt)
1
print(chain.run("fruit"))


​ 1. Seychelles
​ 2. Hawaii
​ 3. Costa Rica
​ 4. Thailand
​ 5. The Caribbean Islands

1
!pip install google-search-results
Successfully installed google-search-results-2.4.2
1
2
3
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.llms import OpenAI
1
2
# load the model 
llm = OpenAI(temperature=0)
1
2
3
# load in some tools to use
os.environ["SERPAPI_API_KEY"] = "46eca3f76a945e3c452903dea2641511a1c512a786cbb0eXXXXXXXXXXX"
tools = load_tools(["serpapi", "llm-math"], llm=llm)
1
2
3
4
5
# Finally let's initalize an agent with
# 1. The tools
# 2. The language model
# 3. The type of agent we wangt to use
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)
1
agent.run("哪个国家是世界上最强大的国家?现在最流行的大预言模型是什么模型?")


​ > Entering new AgentExecutor chain…
​ I need to find out which country is the most powerful and what the most popular big prediction model is.
​ Action: Search
​ Action Input: “most powerful country in the world” and “most popular big prediction model”
​ Observation:”The United States, China and Russia are seen as the world’s most powerful countries based on factors like economics, political influence and military …”]
​ Thought:3m I need to find out more about the most popular big prediction model
​ Action: Search
​ Action Input: “most popular big prediction model”
​ Observation: ‘Top 5 Predictive Analytics Models · Classification Model · Clustering Model · Forecast Model · Outliers Model · Time Series Model.
​ Thought:I now know the final answer
​ Final Answer: 美国、中国和俄罗斯被视为世界上最强大的国家,最流行的大预言模型是分类模型、聚类模型、预测模型、异常值模型和时间序列模型。

​ Finished chain.

​ ‘美国、中国和俄罗斯被视为世界上最强大的国家,最流行的大预言模型是分类模型、聚类模型、预测模型、异常值模型和时间序列模型。’

1.3. Memory: Add state to chains and agents

1
from langchain import OpenAI, ConversationChain
1
2
llm = OpenAI(temperature=0)
conversation = ConversationChain(llm=llm, verbose=True)
1
conversation.predict(input="Hi there!")
> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.

Current conversation:

Human: Hi there!
AI:

> Finished chain.

" Hi there! It's nice to meet you. How can I help you today?"
1
conversation.predict(input="What was the first thing i said to you?")
1
2
3
4
5
6
7
8
9
10
11
> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.

Current conversation:
Human: Hi there!
AI: Hi there! It's nice to meet you. How can I help you today?
Human: What was the first thing i said to you?
AI:

> Finished chain.
' You said "Hi there!"'