
使用DeepSeek和Claude繪制出高質量的SVG 圖片
source gemini_env/bin/activate # On Windows, use: gemini_env\Scripts\activate
pip install google-generativeai
2。配置您的 API 密鑰并導入庫:
在您的 Python 腳本中,設置 Gemini 客戶端:
import google.generativeai as genai
# Replace 'YOUR_API_KEY' with your actual Gemini API key.
genai.configure(api_key="YOUR_API_KEY")
model = genai.GenerativeModel("gemini-2.5-pro-exp-03-25")
該摘要將導入必要的庫,配置 API 鍵,并將模型設置為 Gemini Pro 2.5 實驗。
在構建完整代理之前,請用簡單的提示測試模型 ?
# A simple test prompt
prompt = "Explain the significance of Occam's Razor in simple terms."
response = model.generate_content(prompt)
print(response.text)
通過執行來運行此腳本 ?
python your_script.py
您應該明確,簡潔地解釋 Occam 的剃須刀,這是您的雙子座設置正常工作的一個很好的跡象。
讓我們構建一個可以回答用戶查詢的簡單代理。該代理將:
在此示例中,我們的代理人將充當基本的對話助手。
以下是一個完整的 Python 腳本,可以設置并運行基本的對話循環 ?
import google.generativeai as genai
# Configure the Gemini API with your API key
genai.configure(api_key="YOUR_API_KEY")
model = genai.GenerativeModel("gemini-2.5-pro-exp-03-25")
def ask_agent(prompt):
"""Send a prompt to Gemini Pro 2.5 and return the response."""
response = model.generate_content(prompt)
return response.text
def run_agent():
print("Welcome to the Gemini Pro 2.5 Agent!")
print("Type 'exit' to quit.\n")
while True:
user_input = input("You: ")
if user_input.lower() == "exit":
print("Agent: Goodbye!")
break
# Process the user input and generate a response
answer = ask_agent(user_input)
print(f"Agent: {answer}\n")
if __name__ == "__main__":
run_agent()
ask_agent
: 將用戶提示發送到模型并返回其文本響應。run_agent
: 實現一個簡單的循環,該循環連續接受用戶輸入并顯示代理的答復,直到用戶類型“退出”為止。這個分步示例演示了如何將 Gemini Pro 2.5 集成到您的應用程序中,以構建能夠處理用戶查詢的自主劑。
Gemini Pro 2.5 代表了 AI 模型功能的重大飛躍,這要歸功于其先進的“思考”機制,多模式支持和擴展上下文窗口。無論您是希望自動化編碼任務的開發人員,分析大型數據集的研究人員還是探索新邊界的內容創建者,該模型都提供了前所未有的力量和靈活性。
通過遵循本指南(從安裝到建立基本Agent),您現在有基礎可以進一步實驗。利用官方的演示文稿,并咨詢開發人員文檔以獲取其他見解和高級用例。
文章轉載自:Getting Started with Gemini Pro 2.5: Build a Simple AI Agent