PrefectHQ / ControlFlow

🦾 Take control of your AI agents
https://controlflow.ai
Apache License 2.0
337 stars 15 forks source link

default_model requires OPENAI_API_KEY to be set even with custom LLMs #169

Closed Barneyjm closed 1 week ago

Barneyjm commented 1 week ago

Trying to import the module without OPENAI_API_KEY set throws an error.

controlflow==0.7.5

windows (yuck) powershell (super yuck)

Ideal resolution:

Temporary Resolution:

example script:

import os
from pydantic import BaseModel

from langchain_mycustommodel import MyCustomLLM #type: langchain LLM

llm = MyCustomLLM(**kwargs)

import controlflow as cf
cf.default_model = llm

# also tried: 
author = cf.Agent(
    model=llm,
    name="Deep Thought",
    instructions="Use a formal tone and clear language",
)

throws error:

openai/gpt-4o None
Traceback (most recent call last):
  File "example\cf.py", line 5, in <module>
    import controlflow as cf
  File ".control\Lib\site-packages\controlflow\__init__.py", line 18, in <module>
    default_model = model_from_string(controlflow.settings.llm_model)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".control\Lib\site-packages\controlflow\llm\models.py", line 63, in model_from_string
    return cls(model=model, temperature=temperature, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".control\Lib\site-packages\pydantic\v1\main.py", line 341, in __init__
    raise validation_error
pydantic.v1.error_wrappers.ValidationError: 1 validation error for ChatOpenAI
__root__
  Did not find openai_api_key, please add an environment variable `OPENAI_API_KEY` which contains it, or pass `openai_api_key` as a named parameter. (type=value_error)

caused by the top level import itself: https://github.com/PrefectHQ/ControlFlow/blob/main/src/controlflow/__init__.py#L18

jlowin commented 1 week ago

Thanks for opening! I actually thought we squashed this but clearly not. Hold tight.

jlowin commented 1 week ago

Thanks so much for highlighting -- expect to have 0.8 out tonight, including this fix.