memo: langchain helloworld

支 持 本 站: 捐贈伺服器等運維費用,需要您的支持!

pip install langchain
pip install git+https://github.com/openai/whisper.git

https://python.langchain.com/en/latest/getting_started/getting_started.html

from langchain.agents import load_tools from langchain.agents import initialize_agent from langchain.agents import AgentType from langchain.llms import OpenAI from langchain.prompts import PromptTemplate

# load the language model
llm = OpenAI(temperature=0.9, n=2, best_of=2)

#### generate text
text = "請介紹至少3種學習LLM技術的方法"
print(llm(text))
# ->
#1.網上學習:LLM技術可以快速熟悉和了解,網上學習可以幫助深入了解LLM技術。例如,可以在在線課程和論壇上學習LLM技術,了解程式語言,及其應用。
#2.實踐:實踐是學習LLM技術的重要組成部分,實踐可以幫助學習者更好地理解LLM技術的原理及其應用。

#### number of tokens
# pip install git+https://github.com/openai/whisper.git
print(llm.get_num_tokens("請介紹至少3種學習LLM技術的方法"))
# -> 31

#### generate method
llm_result = llm.generate(["請講個笑話", "請寫首現代詩"]*2)
print(llm_result.generations)

#### manage prompt
prompt = PromptTemplate(
input_variables=["product", "cs_name"],
template="歡迎使用{product}產品, 我是客服{cs_name}, 請問您有什麼問題?",
)
print(prompt.format(product="人工智慧機器人",cs_name="小王"))
# -> '歡迎使用人工智慧機器人產品, 我是客服小王, 請問您有什麼問題?'

from langchain.chains import LLMChain

#### LLMChain: PromptTemplate and an LLM
chain = LLMChain(llm=llm, prompt=prompt)
print(chain.run(product="人工智慧機器人",cs_name="小王"))
# -> '\n\n我想了解你們的產品有哪些特色功能?'


#### Agent
# pip install google-search-results

# Next, let's load some tools to use. Note that the `llm-math` tool uses an LLM, so we need to pass that in.
tools = load_tools(["serpapi", "llm-math"], llm=llm)

# Finally, let's initialize an agent with the tools, the language model, and the type of agent we want to use.
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
# Now let's test it out!
agent.run("北京的面積有多少平方公里? 換算成有多少畝?")

#### ConversationChain
from langchain import OpenAI, ConversationChain

llm = OpenAI(temperature=0)
conversation = ConversationChain(llm=llm, verbose=True)

output = conversation.predict(input="嗨,你好!")
print(output)

output = conversation.predict(input="挺好的。剛剛在和一個AI聊天呢.")
print(output)




支 持 本 站: 捐贈伺服器等運維費用,需要您的支持!
#### Chat Message from langchain.chat_models import ChatOpenAI from langchain.schema import ( AIMessage, HumanMessage, SystemMessage )

chat = ChatOpenAI(temperature=0)

messages = [
SystemMessage(content="你是一位中學語文老師,擅長用通俗易懂的方法講解古文"),
HumanMessage(content="把古文翻譯成現代文. 帝武丁即位,思復興殷,而未得其佐。三年不言,政事決定於冢宰>,以觀國風。武丁夜夢得聖人,名曰說。以夢所見視群臣百吏,皆非也。於是乃使百工營求之野,得說於傅險中。是>時說為胥靡,築於傅險。見於武丁,武丁曰是也。得而與之語,果聖人,舉以為相,殷國大治。故遂以傅險姓之,號>曰傅說。")
]

output = chat(messages)
print(output.content)

#### MessagePromptTemplate
from langchain.chat_models import ChatOpenAI
from langchain.prompts.chat import (
ChatPromptTemplate,
SystemMessagePromptTemplate,
HumanMessagePromptTemplate,
)

chat = ChatOpenAI(temperature=0)

template = "你是一位中學語文老師,擅長把 {input_language} 翻譯成 {output_language}."
system_message_prompt = SystemMessagePromptTemplate.from_template(template)
human_template = "{text}"
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)

chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])

# get a chat completion from the formatted messages
output = chat(chat_prompt.format_prompt(input_language="古文", output_language="現代文", text="帝武丁即位,思復興殷,而未得其佐").to_messages())
print(output.content)
# -> 帝武丁登基後,他想要恢復殷商的輝煌,但是他還沒有找到合適的輔佐之人

# LLMChain model
chain = LLMChain(llm=chat, prompt=chat_prompt)
output = chain.run(input_language="古文", output_language="白話文", text="帝武丁即位,思復興殷,而未得其佐")
print(output)
# -> 帝武丁登基後,他想要重振殷商的國勢,但是他還沒有找到合適的輔佐人選。


支 持 本 站: 捐贈伺服器等運維費用,需要您的支持!

發布時間: