crewAIInc / 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
20.68k stars 2.86k forks source link

[BUG] OpenAI Model not obeying env variable OPENAI_MODEL_NAME #167 #1336

Closed adilazmoon closed 1 month ago

adilazmoon commented 1 month ago

Description

When upgrading to 0.61.0, I found that my usage charges skyrocketed . Investigated further to realise that I was suddenly using gpt-4o instead of gpt-4o-mini. image

Have the following line in my .env file as well OPENAI_MODEL_NAME=gpt-4o-mini

Steps to Reproduce

No code change on my part, only difference was the upgrading of package version from 0.51.0 to 0.61.0.

Expected behavior

Expected to use gpt-4o-mini as in V0.51.0 instead of gpt-4o (I'm assuming that's the default)

Screenshots/Code snippets

image

Operating System

Windows 10

Python Version

3.12

crewAI Version

0.61.0

crewAI Tools Version

NA

Virtual Environment

Poetry

Evidence

NA

Possible Solution

Was the env variable deprecated? If so, then probably just need to update the docs

Additional context

NA

theCyberTech commented 1 month ago

Hi @adilazmoon Could you please send me your crew so I can dig into it, obviously redact anything sensitive

joaomdmoura commented 1 month ago

@adilazmoon good catch, it was not intentional it seems there is a bug, we are patching it now

joaomdmoura commented 1 month ago

@adilazmoon , spent the day today working on a new LLM class that will be introduced in the next version to address all the concerns in this issue, open for feedback before we cut the version.

New docs, still not live: (https://github.com/crewAIInc/crewAI/blob/main/docs/core-concepts/LLMs.md) Some high level description bellow:

Default

By default, crewAI uses the gpt-4o-mini model.

Using Env Vars

It uses environment variables if no LLM is specified:

String Identifier

Aiming at simplicity and supporting who update to version 0.61.0

agent = Agent(llm="gpt-4o", ...)

LLM Instance

List of more providers.

from crewai import LLM

llm = LLM(model="gpt-4", temperature=0.7)
agent = Agent(llm=llm, ...)

LLM Configuration Options

This new LLM class allow you to use an big array of attributes.

Parameter Type Description
model str The name of the model to use (e.g., "gpt-4", "gpt-3.5-turbo", "ollama/llama3.1", more providers)
timeout float, int Maximum time (in seconds) to wait for a response
temperature float Controls randomness in output (0.0 to 1.0)
top_p float Controls diversity of output (0.0 to 1.0)
n int Number of completions to generate
stop str, List[str] Sequence(s) to stop generation
max_tokens int Maximum number of tokens to generate
presence_penalty float Penalizes new tokens based on their presence in the text so far
frequency_penalty float Penalizes new tokens based on their frequency in the text so far
logit_bias Dict[int, float] Modifies likelihood of specified tokens appearing in the completion
response_format Dict[str, Any] Specifies the format of the response (e.g., {"type": "json_object"})
seed int Sets a random seed for deterministic results
logprobs bool Whether to return log probabilities of the output tokens
top_logprobs int Number of most likely tokens to return the log probabilities for
base_url str The base URL for the API endpoint
api_version str The version of the API to use
api_key str Your API key for authentication

Example:

llm = LLM(
    model="gpt-4",
    temperature=0.8,
    max_tokens=150,
    top_p=0.9,
    frequency_penalty=0.1,
    presence_penalty=0.1,
    stop=["END"],
    seed=42,
    base_url="https://api.openai.com/v1",
    api_key="your-api-key-here"
)
agent = Agent(llm=llm, ...)

Using Ollama (Local LLMs)

crewAI supports using Ollama for running open-source models locally:

  1. Install Ollama: ollama.ai
  2. Run a model: ollama run llama2
  3. Configure agent:
    agent = Agent(
    llm=LLM(model="ollama/llama3.1", base_url="http://localhost:11434"),
    ...
    )

Changing the Base API URL

You can change the base API URL for any LLM provider by setting the base_url parameter:

llm = LLM(
    model="custom-model-name",
    base_url="https://api.your-provider.com/v1",
    api_key="your-api-key"
)
agent = Agent(llm=llm, ...)
adilazmoon commented 1 month ago

Looks good, appreciate the effort to fix it asap!

joaomdmoura commented 1 month ago

new version is out v0.63.1 env vars should work again, also ability to control all different attribute for the LLM using the LLM class, if this is still an issue let us know and we can re-open it :) gpt-4o-mini is also back as default