Last active
June 8, 2024 02:25
-
-
Save arungpisyadi/ecde4fb5c6e6ff9ddfa11cad429a136c to your computer and use it in GitHub Desktop.
How to use Perplexity AI API as the LLM Model for CrewAI in FastAPI framework.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from fastapi import FastAPI | |
from fastapi.responses import JSONResponse | |
from fastapi.requests import Request | |
from fastapi.encoders import jsonable_encoder | |
from typing import List | |
from pydantic import BaseModel | |
from crewai import Agent, Task, Crew, Process | |
from langchain_community.chat_models import ChatPerplexity | |
# install fastapi and crewai | |
# set/export the env params for: | |
# 1. OPENAI_API_KEY | |
# 2. PPLX_API_KEY | |
# Run this gist by running `fastapi dev main.py --port {Port Number}` | |
app = FastAPI() | |
class Item(BaseModel): | |
product: str | |
topic: str | |
class AgentResponse(BaseModel): | |
content: str | |
pplx_client = ChatPerplexity(temperature=0, model="llama-3-sonar-large-32k-chat") | |
# Define the CrewAI agent | |
researcher = Agent( | |
role='Senior Researcher', | |
goal='Get as many information as possible about {product}.', | |
verbose=True, | |
backstory=( | |
"Driven by curiosity, you're at the forefront of" | |
"innovation, eager to explore and share knowledge that could change" | |
"the world." | |
), | |
llm=pplx_client, | |
) | |
# Define the CrewAI task | |
research_task = Task( | |
description=( | |
"Identify the next big trend in {topic}." | |
"Focus on identifying pros and cons and the {product}." | |
"Your final report should clearly articulate the key points," | |
"its market opportunities, and potential risks." | |
), | |
expected_output='A comprehensive 3 paragraphs long report on the {product}.', | |
# tools=[search_tool], | |
agent=researcher, | |
) | |
# Define the CrewAI crew | |
crew = Crew( | |
agents=[researcher], | |
tasks=[research_task], | |
process=Process.sequential, | |
memory=True, | |
cache=True, | |
max_rpm=100, | |
share_crew=True | |
) | |
# Define the route for the Perplexity AI API | |
@app.post("/research/product") | |
async def productResearch(item: Item): | |
# print(messages) | |
response = crew.kickoff(inputs={'product': item.product, 'topic': item.topic}) | |
return JSONResponse(content={"content": response}, media_type="application/json") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment