
生成式人工智慧讓系統能夠根據資料和提示建立文字、圖像、程式碼或其他形式的內容。 LangChain 是一個框架,透過編排工作流程、管理提示以及啟用記憶體和工具整合等進階功能,簡化了生成式 AI 模型的使用。
本指南介紹了使用 LangChain 和 Python 開始產生式 AI 所需的關鍵概念和工具。
1.什麼是浪鏈?
LangChain 是一個基於 Python 的框架,用於使用 OpenAI 的 GPT 或 Hugging Face 模型等大型語言模型 (LLM) 建立應用程式。它有幫助:
-
管理提示:建立可重複使用的結構化提示。
-
鍊式工作流程:將多個LLM呼叫合併到一個工作流程中。
-
使用工具:使 AI 模型能夠與 API、資料庫等互動。
-
新增記憶:允許模型記住過去的交互作用。
2.設定您的環境
a) 安裝所需的庫
首先,安裝LangChain和相關庫:
1 | pip install langchain openai python-dotenv streamlit
|
登入後複製
登入後複製
b) 設定您的 OpenAI API 金鑰
- 註冊 OpenAI 帳戶並取得您的 API 金鑰:OpenAI API。
- 在專案目錄中建立一個 .env 檔案並新增 API 金鑰:
1 | OPENAI_API_KEY=your_api_key_here
|
登入後複製
登入後複製
- 使用 dotenv 在 Python 腳本中載入 API 金鑰:
1 2 3 4 5 | from dotenv import load_dotenv
import os
load_dotenv()
openai_api_key = os. getenv ( "OPENAI_API_KEY" )
|
登入後複製
登入後複製
3. LangChain的關鍵概念
a) 提示
提示引導人工智慧產生所需的輸出。 LangChain允許您使用PromptTemplate系統地建立提示。
1 2 3 4 5 6 7 8 9 10 | from langchain.prompts import PromptTemplate
# Define a template
template = "You are an AI that summarizes text. Summarize the following: {text}"
prompt = PromptTemplate(input_variables=[ "text" ], template=template)
# Generate a prompt with dynamic input
user_text = "Artificial Intelligence is a field of study that focuses on creating machines capable of intelligent behavior."
formatted_prompt = prompt.format(text=user_text)
print (formatted_prompt)
|
登入後複製
登入後複製
b) 語言模型
LangChain 與 OpenAI 的 GPT 或 Hugging Face 模型等法學碩士整合。使用 OpenAI GPT 的 ChatOpenAI。
1 2 3 4 5 6 7 8 | from langchain.chat_models import ChatOpenAI
# Initialize the model
chat = ChatOpenAI(temperature=0.7, openai_api_key=openai_api_key)
# Generate a response
response = chat.predict( "What is Generative AI?" )
print (response)
|
登入後複製
登入後複製
c) 鏈條
鏈將多個步驟或任務組合到一個工作流程中。例如,一條鏈可能:
- 總結文件。
- 根據摘要產生問題。
1 2 3 4 5 6 7 8 9 10 11 | from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
# Create a prompt and chain
template = "Summarize the following text: {text}"
prompt = PromptTemplate(input_variables=[ "text" ], template=template)
chain = LLMChain(llm=chat, prompt=prompt)
# Execute the chain
result = chain.run( "Generative AI refers to AI systems capable of creating text, images, or other outputs." )
print (result)
|
登入後複製
登入後複製
d) 記憶
記憶體使模型能夠保留多次交互的上下文。這對於聊天機器人很有用。
1 2 3 4 5 6 7 8 9 10 | from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory
# Initialize memory and the conversation chain
memory = ConversationBufferMemory()
conversation = ConversationChain(llm=chat, memory=memory)
# Have a conversation
print (conversation.run( "Hi, who are you?" ))
print (conversation.run( "What did I just ask you?" ))
|
登入後複製
登入後複製
4.範例應用
a) 文字產生
使用提示產生創意回應或內容。
1 2 3 4 5 6 7 8 9 10 | from langchain.chat_models import ChatOpenAI
from langchain.prompts import PromptTemplate
chat = ChatOpenAI(temperature=0.9, openai_api_key=openai_api_key)
prompt = PromptTemplate(input_variables=[ "topic" ], template= "Write a poem about {topic}." )
chain = LLMChain(llm=chat, prompt=prompt)
# Generate a poem
result = chain.run( "technology" )
print (result)
|
登入後複製
b) 總結
高效率總結文件或文字。
1 | pip install langchain openai python-dotenv streamlit
|
登入後複製
登入後複製
c) 聊天機器人
建立一個具有記憶功能的互動式聊天機器人。
1 | OPENAI_API_KEY=your_api_key_here
|
登入後複製
登入後複製
5.高級功能
a) 工具
使模型能夠存取網路搜尋或資料庫等外部工具。
1 2 3 4 5 | from dotenv import load_dotenv
import os
load_dotenv()
openai_api_key = os. getenv ( "OPENAI_API_KEY" )
|
登入後複製
登入後複製
b) 客製化鏈
透過組合多個任務來建立自訂工作流程。
1 2 3 4 5 6 7 8 9 10 | from langchain.prompts import PromptTemplate
# Define a template
template = "You are an AI that summarizes text. Summarize the following: {text}"
prompt = PromptTemplate(input_variables=[ "text" ], template=template)
# Generate a prompt with dynamic input
user_text = "Artificial Intelligence is a field of study that focuses on creating machines capable of intelligent behavior."
formatted_prompt = prompt.format(text=user_text)
print (formatted_prompt)
|
登入後複製
登入後複製
6.使用 Streamlit 部署
使用 Streamlit 為您的生成式 AI 模型建立一個簡單的 Web 應用程式。
安裝 Streamlit:
1 2 3 4 5 6 7 8 | from langchain.chat_models import ChatOpenAI
# Initialize the model
chat = ChatOpenAI(temperature=0.7, openai_api_key=openai_api_key)
# Generate a response
response = chat.predict( "What is Generative AI?" )
print (response)
|
登入後複製
登入後複製
簡單的應用程式:
1 2 3 4 5 6 7 8 9 10 11 | from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
# Create a prompt and chain
template = "Summarize the following text: {text}"
prompt = PromptTemplate(input_variables=[ "text" ], template=template)
chain = LLMChain(llm=chat, prompt=prompt)
# Execute the chain
result = chain.run( "Generative AI refers to AI systems capable of creating text, images, or other outputs." )
print (result)
|
登入後複製
登入後複製
運行應用程式:
1 2 3 4 5 6 7 8 9 10 | from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory
# Initialize memory and the conversation chain
memory = ConversationBufferMemory()
conversation = ConversationChain(llm=chat, memory=memory)
# Have a conversation
print (conversation.run( "Hi, who are you?" ))
print (conversation.run( "What did I just ask you?" ))
|
登入後複製
登入後複製
7.生成式 AI 開發人員的關鍵概念
a) 微調模型
學習在自訂資料集上微調 GPT 或穩定擴散等模型。
b) 及時工程
掌握如何製作有效的提示以獲得所需的輸出。
c) 多模態人工智慧
使用結合文字、圖像和其他模式的模型(例如 OpenAI 的 DALL·E 或 CLIP)。
d) 擴充與部署
使用雲端服務或 Docker 等工具將模型部署到生產環境。
8.資源
-
LangChain文檔:LangChain文檔
-
OpenAI API:OpenAI 文檔
-
抱臉模特兒:抱臉
遵循本指南,您將獲得使用 Python 和 LangChain 建立生成式 AI 應用程式所需的基礎知識。開始實驗、建立工作流程並深入探索令人興奮的 AI 世界!
以上是使用 LangChain 和 Python 產生人工智慧的綜合初學者指南 - 3的詳細內容。更多資訊請關注PHP中文網其他相關文章!