crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

[BUG] Problems with llm/litellm or api key?

Open ulrmehdi opened this issue 2 years ago • 11 comments

Description

I am trying to build my project and did set it up, as suggested, using the command 'crewai create crew my_project'. I do run this commend in crewAI repo folder. After setting up my project, my_project folder contains subfolders: tests, src and files: myproject.toml, README.md and uv.lock. In src folder, I do have inside of it, again, my_project folder, with subfolders: tools, config and files crew.py and main.py.

The first question: this folder structure is a bit different from the one shown in https://docs.crewai.com/installation, is it a critical issue? Note that also, in the process of setting my project, I selected following options from rundown menus: 6. (other) -> 5. (azure), and 13. (azure/gpt-4o-mini). Answering to the open to set OPENAI_API_KEY, I set it up in .env file.
The second question: if I selected Azure, should not it be set the AZURE_OPENAI_KEY, instead? After setting all agents and tasks, I did try to run crewAI from my project folder, like 'crewai run'. I've got the lines: 'Running the Crew', 'Using CPython 3.11.10 interpreter at: /anaconda/envs/crewai311/bin/python', [0/218] Installing wheels.. I'Creating virtual environment at: .venv, Built my_project @ file....... Then warnings: warning: Failed to hardlink files; falling back to full copy. This may lead to degraded performance. If the cache and target directories are on different filesystems, hardlinking may not be supported. If this is intentional, set export UV_LINK_MODE=copy or use --link-mode=copy to suppress this warning. Installed 218 packages in 4m 24s. Not sure how to react to the warning promptly. Then, crewai attempted to load by Agents and Tasks, and repeated the same loading two more times, each time with the same failure notice shown below.

Again, why there is a reference to openai.com, while it should be azure_openAI instance?? Any suggestion on how to fix this problem? Thank you for your help!

Steps to Reproduce

It is demonstrated above

Expected behavior

tba

Screenshots/Code snippets

no screenshots

Operating System

Ubuntu 20.04

Python Version

3.11

crewAI Version

0.76.2

crewAI Tools Version

0.13.2

Virtual Environment

Conda

Evidence

2024-10-24 18:40:02,339 - 140581769302656 - llm.py-llm:161 - ERROR: LiteLLM call failed: litellm.AuthenticationError: AuthenticationError: OpenAIException - Error code: 401 - {'error': {'message': 'Incorrect API key provided: b***********************2d13. You can find your API key at https://platform.openai.com/account/api-keys.', 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_api_key'}}.        
Traceback (most recent call last):
  File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/work-cpu/code/Users/xxxxx/Projects/AIAgents/crewAI/py_course/.venv/lib/python3.11/site-packages/litellm/llms/OpenAI/openai.py", line 854, in completion
    raise e
  File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/work-cpu/code/Users/xxxxxProjects/AIAgents/crewAI/py_course/.venv/lib/python3.11/site-packages/litellm/llms/OpenAI/openai.py", line 790, in completion
    self.make_sync_openai_chat_completion_request(
  File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/work-cpu/code/Users/xxxxx/Projects/AIAgents/crewAI/py_course/.venv/lib/python3.11/site-packages/litellm/llms/OpenAI/openai.py", line 649, in make_sync_openai_chat_completion_request
    raise e
  File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/work-cpu/code/Users/xxxxx/Projects/AIAgents/crewAI/py_course/.venv/lib/python3.11/site-packages/litellm/llms/OpenAI/openai.py", line 631, in make_sync_openai_chat_completion_request
    raw_response = openai_client.chat.completions.with_raw_response.create(
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/work-cpu/code/Users/xxxxxx/Projects/AIAgents/crewAI/py_course/.venv/lib/python3.11/site-packages/openai/_legacy_response.py", line 356, in wrapped
    return cast(LegacyAPIResponse[R], func(*args, **kwargs))
                                      ^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/work-cpu/code/Users/xxxxxx/Projects/AIAgents/crewAI/py_course/.venv/lib/python3.11/site-packages/openai/_utils/_utils.py", line 274, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/work-cpu/code/Users/xxxxx/Projects/AIAgents/crewAI/py_course/.venv/lib/python3.11/site-packages/openai/resources/chat/completions.py", line 815, in create
    return self._post(
           ^^^^^^^^^^^
  File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/work-cpu/code/Users/xxxxxx/Projects/AIAgents/crewAI/py_course/.venv/lib/python3.11/site-packages/openai/_base_client.py", line 1277, in post
    return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/work-cpu/code/Users/xxxxx/Projects/AIAgents/crewAI/py_course/.venv/lib/python3.11/site-packages/openai/_base_client.py", line 954, in request
    return self._request(
           ^^^^^^^^^^^^^^
  File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/work-cpu/code/Users/xxxxx/Projects/AIAgents/crewAI/py_course/.venv/lib/python3.11/site-packages/openai/_base_client.py", line 1058, in _request
    raise self._make_status_error_from_response(err.response) from None
openai.AuthenticationError: Error code: 401 - {'error': {'message': 'Incorrect API key provided: bbac2b33********************2d13. You can find your API key at https://platform.openai.com/account/api-keys.', 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_api_key'}}

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/work-cpu/code/Users/xxxxx/Projects/AIAgents/crewAI/py_course/.venv/lib/python3.11/site-packages/litellm/main.py", line 1597, in completion
    raise e
  File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/work-cpu/code/Users/xxxxx/Projects/AIAgents/crewAI/py_course/.venv/lib/python3.11/site-packages/litellm/main.py", line 1570, in completion
    response = openai_chat_completions.completion(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/work-cpu/code/Users/81737/Projects/AIAgents/crewAI/py_course/.venv/lib/python3.11/site-packages/litellm/llms/OpenAI/openai.py", line 864, in completion
    raise OpenAIError(
litellm.llms.OpenAI.openai.OpenAIError: Error code: 401 - {'error': {'message': 'Incorrect API key provided: b********************2d13. You can find your API key at https://platform.openai.com/account/api-keys.', 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_api_key'}}

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/work-cpu/code/Users/xxxxxx/Projects/AIAgents/crewAI/py_course/.venv/bin/run_crew", line 10, in <module>
    sys.exit(run())
             ^^^^^
  File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/work-cpu/code/Users/xxxxxxProjects/AIAgents/crewAI/py_course/src/py_course/main.py", line 17, in run
    PyCourseCrew().crew().kickoff(inputs=inputs)
  File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/work-cpu/code/Users/xxxxxx/Projects/AIAgents/crewAI/py_course/.venv/lib/python3.11/site-packages/crewai/crew.py", line 494, in kickoff
    result = self._run_sequential_process()
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/work-cpu/code/Users/xxxxxx/Projects/AIAgents/crewAI/py_course/.venv/lib/python3.11/site-packages/crewai/crew.py", line 598, in _run_sequential_process
    return self._execute_tasks(self.tasks)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/work-cpu/code/Users/xxxxx/Projects/AIAgents/crewAI/py_course/.venv/lib/python3.11/site-packages/crewai/crew.py", line 696, in _execute_tasks
    task_output = task.execute_sync(
                  ^^^^^^^^^^^^^^^^^^
  File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/work-cpu/code/Users/xxxxx/Projects/AIAgents/crewAI/py_course/.venv/lib/python3.11/site-packages/crewai/task.py", line 191, in execute_sync
    return self._execute_core(agent, context, tools)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/work-cpu/code/Users/xxxxxProjects/AIAgents/crewAI/py_course/.venv/lib/python3.11/site-packages/crewai/task.py", line 247, in _execute_core
    result = agent.execute_task(
             ^^^^^^^^^^^^^^^^^^^
  File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/work-cpu/code/Projects/AIAgents/crewAI/py_course/.venv/lib/python3.11/site-packages/crewai/agent.py", line 249, in execute_task
    result = self.execute_task(task, context, tools)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/work-cpu/code/Projects/AIAgents/crewAI/py_course/.venv/lib/python3.11/site-packages/crewai/agent.py", line 249, in execute_task
    result = self.execute_task(task, context, tools)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/work-cpu/code/Projects/AIAgents/crewAI/py_course/.venv/lib/python3.11/site-packages/crewai/agent.py", line 248, in execute_task
    raise e
  File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/work-cpu/code/Projects/AIAgents/crewAI/py_course/.venv/lib/python3.11/site-packages/crewai/agent.py", line 237, in execute_task
    result = self.agent_executor.invoke(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/work-cpu/code/Users/Projects/AIAgents/crewAI/py_course/.venv/lib/python3.11/site-packages/crewai/agents/crew_agent_executor.py", line 93, in invoke
    formatted_answer = self._invoke_loop()
                       ^^^^^^^^^^^^^^^^^^^
  File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/work-cpu/code/Users/Projects/AIAgents/crewAI/py_course/.venv/lib/python3.11/site-packages/crewai/agents/crew_agent_executor.py", line 175, in _invoke_loop
    raise e
  File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/work-cpu/code/Users/Projects/AIAgents/crewAI/py_course/.venv/lib/python3.11/site-packages/crewai/agents/crew_agent_executor.py", line 115, in _invoke_loop
    answer = self.llm.call(
             ^^^^^^^^^^^^^^
  File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/work-cpu/code/Users/Projects/AIAgents/crewAI/py_course/.venv/lib/python3.11/site-packages/crewai/llm.py", line 155, in call
    response = litellm.completion(**params)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/work-cpu/code/Users/Projects/AIAgents/crewAI/py_course/.venv/lib/python3.11/site-packages/litellm/utils.py", line 1013, in wrapper
    raise e
  File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/work-cpu/code/Users/Projects/AIAgents/crewAI/py_course/.venv/lib/python3.11/site-packages/litellm/utils.py", line 903, in wrapper
    result = original_function(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/work-cpu/code/Users/Projects/AIAgents/crewAI/py_course/.venv/lib/python3.11/site-packages/litellm/main.py", line 3009, in completion
    raise exception_type(
          ^^^^^^^^^^^^^^^
  File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/work-cpu/code/Users/81737/Projects/AIAgents/crewAI/py_course/.venv/lib/python3.11/site-packages/litellm/litellm_core_utils/exception_mapping_utils.py", line 2116, in exception_type
    raise e
  File "/mnt/batch/tasks/shared/LS_root/mounts/clusters/work-cpu/code/Users/81737/Projects/AIAgents/crewAI/py_course/.venv/lib/python3.11/site-packages/litellm/litellm_core_utils/exception_mapping_utils.py", line 343, in exception_type
    raise AuthenticationError(
litellm.exceptions.AuthenticationError: litellm.AuthenticationError: AuthenticationError: OpenAIException - Error code: 401 - {'error': {'message': 'Incorrect API key provided: b********************2d13. You can find your API key at https://platform.openai.com/account/api-keys.', 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_api_key'}}
An error occurred while running the crew: Command '['uv', 'run', 'run_crew']' returned non-zero exit status 1.

Possible Solution

?

Additional context

Running the Crew Using CPython 3.11.10 interpreter at: /anaconda/envs/crewai311/bin/python Creating virtual environment at: .venv Built py-course @ file:///mnt/batch/tasks/shared/LS_root/mounts/clusters/rashid-cpu/code/Users/81737/Projects/AIAgents/crewAI/py_course ░░░░░░░░░░░░░░░░░░░░ [0/218] Installing wheels... warning: Failed to hardlink files; falling back to full copy. This may lead to degraded performance. If the cache and target directories are on different filesystems, hardlinking may not be supported. If this is intentional, set export UV_LINK_MODE=copy or use --link-mode=copy to suppress this warning. Installed 218 packages in 4m 24s

ulrmehdi avatar Oct 24 '24 20:10 ulrmehdi

Crewai calls llm based on litellm. You can first check whether your token format can be used normally in litellm.

flingjie avatar Oct 25 '24 08:10 flingjie

Thanks for the tip. Any hints on how I could do that?

Note that I even have those llm complants if I run crewai using your example in https://docs.crewai.com/quickstart:

Resolved 224 packages in 1ms Audited 218 packages in 14ms (crewai311) azureuser@compute-cpu:~/cloudfiles/code/Users/8xxxxx/Projects/AIAgents/crewAI/py_course$ crewai run Running the Crew

Agent: AI LLMs Senior Data Researcher

Task: Conduct a thorough research about AI LLMs Make sure you find any interesting and relevant information given the current year is 2024.

2024-10-25 13:06:36,290 - 140532287615616 - llm.py-llm:161 - ERROR: LiteLLM call failed: litellm.AuthenticationError: AuthenticationError: OpenAIException - Error code: 401 - {'error': {'message': 'Incorrect API key provided: bbac2b33********************2d13. You can find your API key at https://platform.openai.com/account/api-keys.', 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_api_key'}}

Agent: AI LLMs Senior Data Researcher

Task: Conduct a thorough research about AI LLMs Make sure you find any interesting and relevant information given the current year is 2024.

2024-10-25 13:06:36,355 - 140532287615616 - llm.py-llm:161 - ERROR: LiteLLM call failed: litellm.AuthenticationError: AuthenticationError: OpenAIException - Error code: 401 - {'error': {'message': 'Incorrect API key provided: bbac2b33********************2d13. You can find your API key at https://platform.openai.com/account/api-keys.', 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_api_key'}}

My API key (AZURE_OPENAI_API_KEY) I use regularly in my many other (non-crewai) projects just fine. I do not work with OpenAPI, as it is not permitted in my organization due to security reasons. So, how to fix this problem?

Please also note that I did run 'crew install' just before this output, as I have replaced my yaml files, and both main.py and crew.py to match the crew original ones I was getting when I had created a new crew project.

ulrmehdi avatar Oct 25 '24 13:10 ulrmehdi

#1386

from crewai import LLM


llm = LLM(
              model = "<model>",
              base_url = "<url>"
              api_version = "<version>",
              api_key = "<key>",
              azure=True,
)

Does this work for you?

flingjie avatar Oct 26 '24 03:10 flingjie

I am not sure where or how to use your recipe. I do work with yaml files for agents and tasks, and two steering files, main.py and crew.py. My main.py contains these lines: #!/usr/bin/env python import sys from py_course.crew import PyCourseCrew

This main file is intended to be a way for you to run your

crew locally, so refrain from adding unnecessary logic into this file.

Replace with inputs you want to test with, it will automatically

interpolate any tasks and agents information

def run():
""" Run the crew. """ inputs = { 'topic': 'AI LLMs' } PyCourseCrew().crew().kickoff(inputs=inputs)

def train(): """ Train the crew for a given number of iterations. """ inputs = { "topic": "AI LLMs" } try: PyCourseCrew().crew().train(n_iterations=int(sys.argv[1]), filename=sys.argv[2], inputs=inputs)

except Exception as e:
    raise Exception(f"An error occurred while training the crew: {e}") 

Your posting, I guess, related to llm settings, but how that settings fit into the latest https://docs.crewai.com/installation framework?

Am I missing something important?

ulrmehdi avatar Oct 27 '24 22:10 ulrmehdi

Any ideas or hints?

ulrmehdi avatar Oct 28 '24 23:10 ulrmehdi

In crew.py, the above can be passed as parameters to llm.

flingjie avatar Oct 29 '24 01:10 flingjie

Hi @ulrmehdi

When you select Other > 5 > 13 it should ask you to enter a Key name which you should enter AZURE_OPENAI_KEY.

theCyberTech avatar Oct 30 '24 00:10 theCyberTech

Hi, thanks, but I know it and used it to set up exactly this way. I just need a working example of the main.py which would incorporate two agents and, using litellm, would allow me build my project/process. I did set up yaml files for ageints and tasks, I just need a proper coordination of using them in a required way with litellm, or from my Azure OpeAI Chat gpt-4o-mini deployment. Also, as I look at the CrewAI examples, the poetry system is used to manage LLMs, is it necessarily need to be poetry? I am reluctant to bring up too many extra frameworks to complicate a project. Thanks!

ulrmehdi avatar Oct 30 '24 13:10 ulrmehdi

you can close

ulrmehdi avatar Oct 31 '24 16:10 ulrmehdi

I have figured out everything related to this problem, and my project solution works as expected.

ulrmehdi avatar Oct 31 '24 16:10 ulrmehdi

@theCyberTech , how to define the parameters for llm , i tried having decoding_method as sampling , but getting errors , when i define top_k,top_p?

naredlarohithreddy avatar Dec 24 '24 07:12 naredlarohithreddy