从 Portkey 控制面板创建一个带有可选预算/速率限制的 Portkey API Key。您还可以为此密钥附加用于可靠性、缓存等功能的配置。稍后将详细介绍。
3
将 CrewAI 配置为使用 Portkey
集成非常简单 - 您只需更新 CrewAI 设置中的 LLM 配置即可。
from crewai import LLMfrom portkey_ai import createHeaders, PORTKEY_GATEWAY_URL# Create an LLM instance with Portkey integrationgpt_llm = LLM( model="gpt-4o", base_url=PORTKEY_GATEWAY_URL, api_key="dummy", # We are using a Virtual key, so this is a placeholder extra_headers=createHeaders( api_key="YOUR_PORTKEY_API_KEY", virtual_key="YOUR_LLM_VIRTUAL_KEY", trace_id="unique-trace-id", # Optional, for request tracing ))#Use them in your Crew Agents like this: @agent def lead_market_analyst(self) -> Agent: return Agent( config=self.agents_config['lead_market_analyst'], verbose=True, memory=False, llm=gpt_llm )
提示词沙盒是一个用于比较、测试和部署 AI 应用提示词的场所。您可以在此试验不同的模型、测试变量、比较输出,并在部署到生产环境之前优化您的提示词工程策略。它允许您:
在智能体中使用前进行迭代开发提示词
使用不同的变量和模型测试提示词
比较不同提示词版本之间的输出
与团队成员协作开发提示词
这种可视化环境使得为 CrewAI 智能体工作流的每个步骤打造有效的提示词变得更加容易。
提示词渲染 API 可获取配置好所有参数的提示词模板。
from crewai import Agent, LLMfrom portkey_ai import createHeaders, PORTKEY_GATEWAY_URL, Portkey# Initialize Portkey admin clientportkey_admin = Portkey(api_key="YOUR_PORTKEY_API_KEY")# Retrieve prompt using the render APIprompt_data = portkey_client.prompts.render( prompt_id="YOUR_PROMPT_ID", variables={ "agent_role": "Senior Research Scientist", })backstory_agent_prompt=prompt_data.data.messages[0]["content"]# Set up LLM with Portkey integrationportkey_llm = LLM( model="gpt-4o", base_url=PORTKEY_GATEWAY_URL, api_key="dummy", extra_headers=createHeaders( api_key="YOUR_PORTKEY_API_KEY", virtual_key="YOUR_OPENAI_VIRTUAL_KEY" ))# Create agent using the rendered promptresearcher = Agent( role="Senior Research Scientist", goal="Discover groundbreaking insights about the assigned topic", backstory=backstory_agent, # Use the rendered prompt verbose=True, llm=portkey_llm)
您可以:
创建同一提示词的多个版本
比较版本间的性能
必要时回滚到之前的版本
在代码中指定要使用的版本
# Use a specific prompt versionprompt_data = portkey_admin.prompts.render( prompt_id="YOUR_PROMPT_ID@version_number", variables={ "agent_role": "Senior Research Scientist", "agent_goal": "Discover groundbreaking insights" })
Portkey 提示词使用 Mustache 风格的模板,便于变量替换。
You are a {{agent_role}} with expertise in {{domain}}.Your mission is to {{agent_goal}} by leveraging your knowledgeand experience in the field.Always maintain a {{tone}} tone and focus on providing {{focus_area}}.
from crewai import Agent, LLMfrom portkey_ai import createHeaders, PORTKEY_GATEWAY_URL# Configure LLM with user trackingportkey_llm = LLM( model="gpt-4o", base_url=PORTKEY_GATEWAY_URL, api_key="dummy", extra_headers=createHeaders( api_key="YOUR_PORTKEY_API_KEY", virtual_key="YOUR_OPENAI_VIRTUAL_KEY", metadata={ "_user": "user_123", # Special _user field for user analytics "user_tier": "premium", "user_company": "Acme Corp", "session_id": "abc-123" } ))# Create agent with tracked LLMresearcher = Agent( role="Senior Research Scientist", goal="Discover groundbreaking insights about the assigned topic", backstory="You are an expert researcher with deep domain knowledge.", verbose=True, llm=portkey_llm)
from crewai import Agent, LLMfrom portkey_ai import createHeaders, PORTKEY_GATEWAY_URL# Set up LLMs with different providersopenai_llm = LLM( model="gpt-4o", base_url=PORTKEY_GATEWAY_URL, api_key="dummy", extra_headers=createHeaders( api_key="YOUR_PORTKEY_API_KEY", virtual_key="YOUR_OPENAI_VIRTUAL_KEY" ))anthropic_llm = LLM( model="claude-3-5-sonnet-latest", max_tokens=1000, base_url=PORTKEY_GATEWAY_URL, api_key="dummy", extra_headers=createHeaders( api_key="YOUR_PORTKEY_API_KEY", virtual_key="YOUR_ANTHROPIC_VIRTUAL_KEY" ))# Choose which LLM to use for each agent based on your needsresearcher = Agent( role="Senior Research Scientist", goal="Discover groundbreaking insights about the assigned topic", backstory="You are an expert researcher with deep domain knowledge.", verbose=True, llm=openai_llm # Use anthropic_llm for Anthropic)
from crewai import Agent, LLMfrom portkey_ai import PORTKEY_GATEWAY_URL# Configure LLM with your API keyportkey_llm = LLM( model="gpt-4o", base_url=PORTKEY_GATEWAY_URL, api_key="YOUR_PORTKEY_API_KEY")# Create agent with Portkey-enabled LLMresearcher = Agent( role="Senior Research Scientist", goal="Discover groundbreaking insights about the assigned topic", backstory="You are an expert researcher with deep domain knowledge.", verbose=True, llm=portkey_llm)