Likith
Likith

Reputation: 71

Getting OpenAI API Error While Using Groq API for Python Coding Agent

I am trying to create a Python coding agent using the crewai library along with langchain and ChatGroq. My code is supposed to use the Groq API for language model processing, but I am encountering an error related to OpenAI's API instead

Here's my code:

import logging
from crewai import Agent, Task, Crew, Process
from langchain.agents import Tool
from langchain_experimental.utilities import PythonREPL
from langchain_community.tools.ddg_search.tool import DuckDuckGoSearchRun
from langchain_groq import ChatGroq

# Ensure correct API key and model are set
llm = ChatGroq(temperature=0, api_key="MY_API_KEY", model="llama3-70b-8192")

# Create the Python REPL tool
python_repl = PythonREPL()
python_repl_tool = Tool(
    name="python_repl",
    description="A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.",
    func=python_repl.run,
)

# Create the DuckDuckGo search tool
duckduckgo_search = DuckDuckGoSearchRun()
duckduckgo_search_tool = Tool(
    name="duckduckgo_search",
    description="A wrapper around DuckDuckGo Search. Useful for when you need to answer questions about current events. Input should be a search query.",
    func=duckduckgo_search.run,
)

coderAgent = Agent(
    role='Senior Software engineer and developer',
    goal='Write production grade bug free code on this user prompt :- {topic}',
    verbose=True,
    memory=True,
    backstory=(
        "You are an experienced developer in big tech companies"
        "You have a record of writing bug-free python code all the time and delivering extraordinary programming logic"
        "You are extremely good at python programming "
    ),
    llm=llm,  # Optional
    max_iter=10,  # Optional
    max_rpm=10,
    tools=[duckduckgo_search_tool],
    allow_delegation=True
)

# Creating a writer agent with custom tools and delegation capability
DebuggerAgent = Agent(
    role='Code Debugger and bug solving agent',
    goal='You debug the code line by line and solve bugs and errors in the code by using Python_repl tool which can execute python code and give feedback',
    verbose=True,
    memory=True,
    backstory=(
        "You are a debugger agent you have access to a python interpreter which can run python code and give feedback"
        "You also have internet searching capabilities if you are unable to solve the bug you can search on the internet to solve that bug"
    ),
    tools=[duckduckgo_search_tool, python_repl_tool],
    llm=llm, 
    max_iter=10, 
    max_rpm=10,
    allow_delegation=True
)

# Research task
coding_task = Task(
    description=(
        "Write code in this {topic}."
        "Focus on writing bug-free and production-grade code all the time"
        "You are extremely good in python programming language"
        "You should only return code"
    ),
    expected_output='A Bug-free and production-grade code on {topic}',
    tools=[duckduckgo_search_tool],
    llm=llm,
    agent=coderAgent,
)

# Writing task with language model configuration
debug_task = Task(
    description=(
        "You should run the python code given by the CoderAgent and Check for bugs and errors"
        "If you find any bugs or errors then give feedback to the coderAgent to write code again this is the bug"
        "Always delegate the work if the executed python code gives error"
    ),
    expected_output='you should communicate to CoderAgent and give feedback on the code if the code got error while execution',
    tools=[duckduckgo_search_tool, python_repl_tool],
    agent=DebuggerAgent,
    llm=llm,
    # output_file='temp.py'  # Example of output customization
)

# Forming the tech-focused crew with some enhanced configurations
crew = Crew(
    agents=[coderAgent, DebuggerAgent],
    tasks=[coding_task, debug_task],
    process=Process.sequential,  # Optional: Sequential task execution is default
    memory=True,
    cache=True,
    max_rpm=25,
    share_crew=True
)

# Starting the task execution process with enhanced feedback
result = crew.kickoff(inputs={'topic': 'Write me code for maximum subarray sum in an Array using python and Dont you any helper methods to solve this just pure python'})
print(result)

However, I am encountering the following error:

  File "d:\streamlit\codestral-python-tool\Codestral-python-agent\x.py", line 101, in <module>
    result = crew.kickoff(inputs={'topic': 'Write me code for maximum subarray sum in an Array using python and Dont you any helper methods to solve this just pure python'})
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\streamlit\codestral-python-tool\Codestral-python-agent\.venv\Lib\site-packages\crewai\crew.py", line 264, in kickoff
    result = self._run_sequential_process()
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\streamlit\codestral-python-tool\Codestral-python-agent\.venv\Lib\site-packages\crewai\crew.py", line 305, in _run_sequential_process
    output = task.execute(context=task_output)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\streamlit\codestral-python-tool\Codestral-python-agent\.venv\Lib\site-packages\crewai\task.py", line 183, in execute
    result = self._execute(
             ^^^^^^^^^^^^^^
  File "D:\streamlit\codestral-python-tool\Codestral-python-agent\.venv\Lib\site-packages\crewai\task.py", line 192, in _execute
    result = agent.execute_task(
             ^^^^^^^^^^^^^^^^^^^
  File "D:\streamlit\codestral-python-tool\Codestral-python-agent\.venv\Lib\site-packages\crewai\agent.py", line 222, in execute_task
    memory = contextual_memory.build_context_for_task(task, context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\streamlit\codestral-python-tool\Codestral-python-agent\.venv\Lib\site-packages\crewai\memory\contextual\contextual_memory.py", line 24, in build_context_for_task
    context.append(self._fetch_stm_context(query))
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\streamlit\codestral-python-tool\Codestral-python-agent\.venv\Lib\site-packages\crewai\memory\contextual\contextual_memory.py", line 33, in _fetch_stm_context
    stm_results = self.stm.search(query)
                  ^^^^^^^^^^^^^^^^^^^^^^
  File "D:\streamlit\codestral-python-tool\Codestral-python-agent\.venv\Lib\site-packages\crewai\memory\short_term\short_term_memory.py", line 23, in search
    return self.storage.search(query=query, score_threshold=score_threshold)  # type: ignore # BUG? The reference is to the parent class, but the parent class does not have this parameters
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\streamlit\codestral-python-tool\Codestral-python-agent\.venv\Lib\site-packages\crewai\memory\storage\rag_storage.py", line 95, in search       
    else self.app.search(query, limit)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\streamlit\codestral-python-tool\Codestral-python-agent\.venv\Lib\site-packages\embedchain\embedchain.py", line 653, in search
    return [{"context": c[0], "metadata": c[1]} for c in self.db.query(**params)]
                                                         ^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\streamlit\codestral-python-tool\Codestral-python-agent\.venv\Lib\site-packages\embedchain\vectordb\chroma.py", line 220, in query
    result = self.collection.query(
             ^^^^^^^^^^^^^^^^^^^^^^
  File "D:\streamlit\codestral-python-tool\Codestral-python-agent\.venv\Lib\site-packages\chromadb\api\models\Collection.py", line 327, in query
    valid_query_embeddings = self._embed(input=valid_query_texts)
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\streamlit\codestral-python-tool\Codestral-python-agent\.venv\Lib\site-packages\chromadb\api\models\Collection.py", line 633, in _embed
    return self._embedding_function(input=input)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\streamlit\codestral-python-tool\Codestral-python-agent\.venv\Lib\site-packages\chromadb\api\types.py", line 193, in __call__
    result = call(self, input)
             ^^^^^^^^^^^^^^^^^
  File "D:\streamlit\codestral-python-tool\Codestral-python-agent\.venv\Lib\site-packages\chromadb\utils\embedding_functions.py", line 201, in __call__   
    embeddings = self._client.create(
                 ^^^^^^^^^^^^^^^^^^^^
  File "D:\streamlit\codestral-python-tool\Codestral-python-agent\.venv\Lib\site-packages\openai\resources\embeddings.py", line 114, in create
    return self._post(
           ^^^^^^^^^^^
  File "D:\streamlit\codestral-python-tool\Codestral-python-agent\.venv\Lib\site-packages\openai\_base_client.py", line 1240, in post
    return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\streamlit\codestral-python-tool\Codestral-python-agent\.venv\Lib\site-packages\openai\_base_client.py", line 921, in request
    return self._request(
           ^^^^^^^^^^^^^^
  File "D:\streamlit\codestral-python-tool\Codestral-python-agent\.venv\Lib\site-packages\openai\_base_client.py", line 1020, in _request
    raise self._make_status_error_from_response(err.response) from None
openai.AuthenticationError: Error code: 401 - {'error': {'message': 'Incorrect API key provided: fake. You can find your API key at https://platform.openai.com/account/api-keys.', 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_api_key'}}

I don't understand why an OpenAI API error is being triggered since my code is configured to use the Groq API. I have verified that my API key is correct and have even generated new keys and used them, but the issue persists.

My request:

Thank you!

Upvotes: 0

Views: 1192

Answers (2)

Jaiwardhan Tyagi
Jaiwardhan Tyagi

Reputation: 1

sometimes, if error still occurs, we just define the llm model in all of the agents, as they have been assigned a default model.

Upvotes: 0

Likith
Likith

Reputation: 71

Actually I solved it !! because Crew AI uses so many functionalities from OpenAI by default so if we export OpenAI key the above code will work

OR we can just keep it as env var :-

import os
os.environ["OPENAI_API_KEY"] = "Your_api"

Upvotes: 3

Related Questions