创建conda环境
conda create -n crew python=3.10
pip install -U phidata
pip install -U pgvector pypdf "psycopg[binary]" sqlalchemy
运行docker,获得向量数据库
docker run -d \
-e POSTGRES_DB=ai \
-e POSTGRES_USER=ai \
-e POSTGRES_PASSWORD=ai \
-e PGDATA=/var/lib/postgresql/data/pgdata \
-v pgvolume:/var/lib/postgresql/data \
-p 5532:5432 \
--name pgvector \
phidata/pgvector:16
运行下面的代码将pdf向量化
from phi.assistant import Assistant
from phi.llm.openai import OpenAIChat
from phi.tools.yfinance import YFinanceTools
from phi.llm.groq import Groq
from phi.knowledge.pdf import PDFKnowledgeBase, PDFReader
from phi.vectordb.pgvector import PgVector2
pdf_knowledge_base = PDFKnowledgeBase(
path="data/pdfs",
# Table name: ai.pdf_documents
vector_db=PgVector2(
collection="pdf_documents",
db_url="postgresql+psycopg://ai:ai@localhost:5532/ai",
),
reader=PDFReader(chunk=True),
)
assistant = Assistant(
knowledge_base=pdf_knowledge_base,
add_references_to_prompt=True,
)
assistant.knowledge_base.load(recreate=False)
assistant.print_response("show me some Question-answering prompts")
安装新的依赖
pip install crewai
pip install 'crewai[tools]'
pip install anthropic
pip install langchain_anthropic
pip install langchain_openai
运行代码,获得你先要的提示词
import os
import asyncio
from crewai import Agent, Task, Crew
from langchain_openai import ChatOpenAI
from dotenv import load_dotenv
from crewai_tools import PGSearchTool
# 加载环境变量,确保API密钥等敏感信息不直接暴露在代码中
load_dotenv()
# 获取openai API密钥
api_key = os.environ.get("OPENAI_API_KEY")
if not api_key:
raise ValueError("OPENAI_API_KEY not found in environment variables")
# 初始化PGSearchTool,用于从PostgreSQL数据库中搜索相关内容
search_tool = PGSearchTool(db_uri='postgresql://ai:ai@localhost:5532/ai', table_name='pdf_documents')
def create_agents(task_type):
"""
创建四个不同角色的AI代理
:param task_type: 任务类型
:return: 包含四个Agent对象的列表
"""
# 初始化ChatOpenAI模型
llm = ChatOpenAI(
model_name="gpt-4o-mini",
temperature=0.7,
openai_api_key=api_key,
)
# llm=ChatAnthropic(
# model="gpt-4o-mini",
# api_key = api_key
# )
# 创建Prompt分析师代理
prompt_analyst = Agent(
role='Prompt Analyst',
goal=f'Analyze existing prompts for {task_type} tasks and suggest improvements',
backstory='You are an expert in prompt engineering with years of experience in optimizing prompts for various AI models.',
verbose=True,
allow_delegation=False,
llm=llm,
tools=[search_tool]
)
# 创建Prompt优化师代理
prompt_optimizer = Agent(
role='Prompt Optimizer',
goal=f'Optimize prompts for {task_type} tasks based on analysis and best practices',
backstory='You are a skilled prompt engineer who can refine and enhance prompts to improve AI model performance.',
verbose=True,
allow_delegation=False,
llm=llm,
tools=[search_tool]
)
# 创建Prompt测试员代理
prompt_tester = Agent(
role='Prompt Tester',
goal=f'Test optimized prompts for {task_type} tasks and provide performance feedback',
backstory='You are an experienced QA specialist in AI, focusing on evaluating the effectiveness of prompts.',
verbose=True,
allow_delegation=False,
llm=llm
)
# 创建最终优化专家代理
ultimate_optimizer = Agent(
role='Ultimate Optimizer',
goal=f'Provide the final, most optimized prompt for {task_type} tasks',
backstory='You are a seasoned AI prompt expert with a keen eye for detail and a deep understanding of user requirements.',
verbose=True,
allow_delegation=False,
llm=llm
)
return [prompt_analyst, prompt_optimizer, prompt_tester, ultimate_optimizer]
def create_tasks(agents, task_type, original_prompt, user_requirements):
"""
为每个代理创建相应的任务
:param agents: 代理列表
:param task_type: 任务类型
:param original_prompt: 原始提示词
:param user_requirements: 用户特定要求
:return: 任务列表
"""
tasks = [
# Prompt分析任务
Task(
description=f'''Analyze the given prompt and similar examples from the database. Consider:
1. Clarity and specificity
2. Alignment with intended task ({task_type})
3. Potential ambiguities or misinterpretations
4. Structure and formatting
Original prompt: "{original_prompt}"
Use the search tool to find relevant prompt examples for {task_type} tasks.
Present your analysis in Markdown format.''',
agent=agents[0],
expected_output="A detailed analysis of the prompt and relevant examples in Markdown format"
),
# Prompt优化任务
Task(
description=f'''Based on the analysis, optimize the prompt. Your optimization should:
1. Improve clarity and reduce ambiguity
2. Enhance alignment with the {task_type} task
3. Incorporate effective elements from example prompts
4. Consider the following specific requirements: {user_requirements}
Original prompt: "{original_prompt}"
Use the search tool to find best practices for {task_type} prompts.
Provide the optimized prompt with explanations for your changes.''',
agent=agents[1],
expected_output="An optimized prompt with explanations for changes made"
),
# Prompt测试任务
Task(
description=f'''Test the optimized prompt against the original. Your test should:
1. Compare potential performance on the {task_type} task
2. Evaluate clarity and ease of understanding
3. Assess potential for misuse or unintended outputs
4. Verify that it meets the following requirements: {user_requirements}
Original prompt: "{original_prompt}"
Optimized prompt: [Insert optimized prompt from previous task]
Present your findings in a clear, Markdown-formatted report.''',
agent=agents[2],
expected_output="A comprehensive test report comparing the original and optimized prompts in Markdown format"
),
# 最终优化任务
Task(
description=f'''Review all previous outputs and provide the ultimate optimized prompt. Your task includes:
1. Analyzing the initial prompt, optimized prompt, and test results
2. Ensuring all user requirements are met: {user_requirements}
3. Further refining the prompt based on all available information
4. Providing a final, highly optimized prompt for the {task_type} task
Present your final optimized prompt along with a brief explanation of your refinements.''',
agent=agents[3],
expected_output="The final, most optimized prompt with explanations for refinements"
)
]
return tasks
async def main():
"""
主函数,处理用户输入并启动优化流程
"""
print("Welcome to the Prompt Optimization Assistant!")
# 获取用户输入
original_prompt = input("Please enter the prompt you want to optimize: ")
task_type = input("What type of task is this prompt for? ")
user_requirements = input("Please enter any specific requirements for the optimized prompt: ")
print(f"\nThank you! Starting the optimization process for a {task_type} prompt with the following requirements: {user_requirements}\n")
# 创建代理和任务
agents = create_agents(task_type)
tasks = create_tasks(agents, task_type, original_prompt, user_requirements)
# 创建并启动Crew
crew = Crew(
agents=agents,
tasks=tasks,
verbose=2
)
# 异步执行Crew的kickoff方法
result = await asyncio.to_thread(crew.kickoff)
if __name__ == "__main__":
# 运行主函数
asyncio.run(main())
稍后我会更新如何用本地大模型来完成这个过程
记得提前pip install langchain_ollama
import os
import asyncio
from crewai import Agent, Task, Crew
# from langchain_openai import ChatOpenAI
from langchain_ollama import ChatOllama
from dotenv import load_dotenv
from crewai_tools import PGSearchTool
os.environ["OPENAI_API_KEY"] = "NA"
# 加载环境变量,确保API密钥等敏感信息不直接暴露在代码中
load_dotenv()
# 获取openai API密钥
# api_key = os.environ.get("OPENAI_API_KEY")
# if not api_key:
# raise ValueError("OPENAI_API_KEY not found in environment variables")
# 初始化PGSearchTool,用于从PostgreSQL数据库中搜索相关内容
search_tool = PGSearchTool(db_uri='postgresql://ai:ai@localhost:5532/ai', table_name='pdf_documents')
def create_agents(task_type):
"""
创建四个不同角色的AI代理
:param task_type: 任务类型
:return: 包含四个Agent对象的列表
"""
# 初始化ChatOpenAI模型
# llm = ChatOpenAI(
# model_name="gpt-4o-mini",
# temperature=0.7,
# openai_api_key=api_key,
# )
# llm=ChatAnthropic(
# model="gpt-4o-mini",
# api_key = api_key
# )
llm = ChatOllama(
model = "llama3.1",
base_url = "http://192.168.1.138:11434")
# 创建Prompt分析师代理
prompt_analyst = Agent(
role='Prompt Analyst',
goal=f'Analyze existing prompts for {task_type} tasks and suggest improvements',
backstory='You are an expert in prompt engineering with years of experience in optimizing prompts for various AI models.',
verbose=True,
allow_delegation=False,
llm=llm,
tools=[search_tool]
)
# 创建Prompt优化师代理
prompt_optimizer = Agent(
role='Prompt Optimizer',
goal=f'Optimize prompts for {task_type} tasks based on analysis and best practices',
backstory='You are a skilled prompt engineer who can refine and enhance prompts to improve AI model performance.',
verbose=True,
allow_delegation=False,
llm=llm,
tools=[search_tool]
)
# 创建Prompt测试员代理
prompt_tester = Agent(
role='Prompt Tester',
goal=f'Test optimized prompts for {task_type} tasks and provide performance feedback',
backstory='You are an experienced QA specialist in AI, focusing on evaluating the effectiveness of prompts.',
verbose=True,
allow_delegation=False,
llm=llm
)
# 创建最终优化专家代理
ultimate_optimizer = Agent(
role='Ultimate Optimizer',
goal=f'Provide the final, most optimized prompt for {task_type} tasks',
backstory='You are a seasoned AI prompt expert with a keen eye for detail and a deep understanding of user requirements.',
verbose=True,
allow_delegation=False,
llm=llm
)
return [prompt_analyst, prompt_optimizer, prompt_tester, ultimate_optimizer]
def create_tasks(agents, task_type, original_prompt, user_requirements):
"""
为每个代理创建相应的任务
:param agents: 代理列表
:param task_type: 任务类型
:param original_prompt: 原始提示词
:param user_requirements: 用户特定要求
:return: 任务列表
"""
tasks = [
# Prompt分析任务
Task(
description=f'''Analyze the given prompt and similar examples from the database. Consider:
1. Clarity and specificity
2. Alignment with intended task ({task_type})
3. Potential ambiguities or misinterpretations
4. Structure and formatting
Original prompt: "{original_prompt}"
Use the search tool to find relevant prompt examples for {task_type} tasks.
Present your analysis in Markdown format.''',
agent=agents[0],
expected_output="A detailed analysis of the prompt and relevant examples in Markdown format"
),
# Prompt优化任务
Task(
description=f'''Based on the analysis, optimize the prompt. Your optimization should:
1. Improve clarity and reduce ambiguity
2. Enhance alignment with the {task_type} task
3. Incorporate effective elements from example prompts
4. Consider the following specific requirements: {user_requirements}
Original prompt: "{original_prompt}"
Use the search tool to find best practices for {task_type} prompts.
Provide the optimized prompt with explanations for your changes.''',
agent=agents[1],
expected_output="An optimized prompt with explanations for changes made"
),
# Prompt测试任务
Task(
description=f'''Test the optimized prompt against the original. Your test should:
1. Compare potential performance on the {task_type} task
2. Evaluate clarity and ease of understanding
3. Assess potential for misuse or unintended outputs
4. Verify that it meets the following requirements: {user_requirements}
Original prompt: "{original_prompt}"
Optimized prompt: [Insert optimized prompt from previous task]
Present your findings in a clear, Markdown-formatted report.''',
agent=agents[2],
expected_output="A comprehensive test report comparing the original and optimized prompts in Markdown format"
),
# 最终优化任务
Task(
description=f'''Review all previous outputs and provide the ultimate optimized prompt. Your task includes:
1. Analyzing the initial prompt, optimized prompt, and test results
2. Ensuring all user requirements are met: {user_requirements}
3. Further refining the prompt based on all available information
4. Providing a final, highly optimized prompt for the {task_type} task
Present your final optimized prompt along with a brief explanation of your refinements.''',
agent=agents[3],
expected_output="The final, most optimized prompt with explanations for refinements"
)
]
return tasks
async def main():
"""
主函数,处理用户输入并启动优化流程
"""
print("Welcome to the Prompt Optimization Assistant!")
# 获取用户输入
original_prompt = input("Please enter the prompt you want to optimize: ")
task_type = input("What type of task is this prompt for? ")
user_requirements = input("Please enter any specific requirements for the optimized prompt: ")
print(f"\nThank you! Starting the optimization process for a {task_type} prompt with the following requirements: {user_requirements}\n")
# 创建代理和任务
agents = create_agents(task_type)
tasks = create_tasks(agents, task_type, original_prompt, user_requirements)
# 创建并启动Crew
crew = Crew(
agents=agents,
tasks=tasks,
verbose=2
)
# 异步执行Crew的kickoff方法
result = await asyncio.to_thread(crew.kickoff)
if __name__ == "__main__":
# 运行主函数
asyncio.run(main())