joaomdmoura / crewAI

Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.
https://crewai.com
MIT License
16.74k stars 2.26k forks source link

Perplexity AI api in Crewai #245

Open Adamchanadam opened 4 months ago

Adamchanadam commented 4 months ago

Does any one is using Perplexity AI (pplx-api) in Crewai ? Any sample code can share ?

I attempt with LiteLLM also failed , it return 'value is not a valid dict (type=type_error.dict) [type=value_error, input_value={'role': 'Information Res... at 0x000002493D6CE7A0>}, input_type=dict]). I don't know what should be assign to the 'Agent' llm= .

Haripritamreddy commented 4 months ago

@Adamchanadam Can you send your code here?

Adamchanadam commented 4 months ago

@Adamchanadam Can you send your code here? Sure , here 's the snippets , I tried to use request or LiteLLM , but still doesn't figure out how to overcome the 'researcher = Agent(' issue , pydantic_self.pydantic_validator.validate_python(data, self_instance=__pydantic_self__) pydantic_core._pydantic_core.ValidationError: 1 validation error for Agent Value error, 1 validation error for ConversationSummaryMemory llm value is not a valid dict (type=type_error.dict) [type=value_error, input_value={'role': 'Information Res... at 0x0000019A4BD00680>}, input_type=dict] For further information visit https://errors.pydantic.dev/2.5/v/value_error


import requests

def pplx_client(messages):
    payload = {
        "model": "pplx-7b-chat",
        "messages": messages,
    }
    headers = {
        "Authorization": f"Bearer {os.environ['PERPLEXITYAI_API_KEY']}",
        "Content-Type": "application/json"
    }
    response = requests.post("https://api.perplexity.ai/chat/completions", json=payload, headers=headers)
    if response.status_code == 200:
        response_data = response.json()
        if response_data["choices"]:
            message_content = response_data["choices"][0]["message"]["content"]
            return {"role": "assistant", "content": message_content}
        else:
            return {"role": "assistant", "content": "No response found."}
    else:
        print("Failed to communicate with Perplexity AI API")
        return {"role": "assistant", "content": "Communication with Perplexity AI API failed."}

researcher = Agent(
    role='Information Researcher',
    goal=f'Search the internet for the latest news , announcement about {research_topic} with accurate sourcing, NEVER provide fabricated URLs or hypothetical scenarios.',
    backstory="""As a Researcher, passionate about technology industry's forefront, you meticulously gather and verify the latest business trend and products developments. Your quest for truth requires verifying the authenticity of information, ensuring a credible knowledge base for informed decision-making.""",
    verbose=True,
    llm=pplx_client,
)
yrwrstnhtmr commented 4 months ago

I used this to create agents using PPLX https://github.com/mochan-b/perplexity-ai-langchain/blob/main/perplexity_ai_llm.py

Nc667 commented 3 months ago

I'm trying to run this code as well as the files in the link aforementioned, but I get an error saying that : [ModuleNotFoundError: No module named 'requests']. What should I do?

tsindot commented 3 months ago

I'm trying to run this code as well as the files in the link aforementioned, but I get an error saying that : [ModuleNotFoundError: No module named 'requests']. What should I do?

@Nc667 I looks like you may not have the requests package installed:

$ python -m pip install requests

or add to your requirements.txt or poetry pyproject.toml file.

HAFDIAHMED commented 2 months ago

hello guys i need to knwo how to implement crew ai agents with prepexlity api key ????

Haripritamreddy commented 2 months ago

Have you tried using langchain? I don't use perplexity api but it is compatable with open ai api. So it should work like this

import os
from langchain_openai import ChatOpenAI
import requests
from crewai import Agent, Task, Crew, Process

pplx_client = ChatOpenAI(openai_api_key="API_KEY",openai_api_base="https://api.groq.com/openai/v1", model="mixtral-8x7b-32768")

researcher = Agent(
    role='Information Researcher',
    goal=f'Search the internet for the latest news , announcement about the topic  with accurate sourcing, NEVER provide fabricated URLs or hypothetical scenarios.',
    backstory="""As a Researcher, passionate about technology industry's forefront, you meticulously gather and verify the latest business trend and products developments. Your quest for truth requires verifying the authenticity of information, ensuring a credible knowledge base for informed decision-making.""",
    verbose=True,
    llm=pplx_client,
)

task = Task(
  description="""Your topic is LLM's advancements.""",
  expected_output='A bullet list summary of the top 5 most important AI news',
  agent=researcher
)

crew = Crew(
  agents=[researcher],
  tasks=[task],
  verbose=2, 
)

# Get your crew to work!
result = crew.kickoff()

print("######################")
print(result)

Here in crewai documentation it mentions the same.

Haripritamreddy commented 2 months ago

Or you can you perplexity directly from langchain like this.

Nc667 commented 1 month ago

I've been able to put it like this and it has worked. Thank you! from langchain_community.chat_models import ChatPerplexity from langchain_core.prompts import ChatPromptTemplate