Skip to content

added support for openrouter #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions workflows/.env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# We support all langchain models, openai only for demo purposes
OPENAI_API_KEY=
# We support all openrouter models and langchain openai for demo purposes
OPENAI_API_KEY=
OPENROUTER_API_KEY=
21 changes: 15 additions & 6 deletions workflows/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from workflow_use.builder.service import BuilderService
from workflow_use.controller.service import WorkflowController
from workflow_use.llm.openrouter import ChatOpenRouter
from workflow_use.recorder.service import RecordingService # Added import
from workflow_use.workflow.service import Workflow

Expand All @@ -29,14 +30,22 @@

# Default LLM instance to None
llm_instance = None

try:
llm_instance = ChatOpenAI(model='gpt-4o')
if os.getenv("OPENROUTER_API_KEY"):
llm_instance = ChatOpenRouter(model="openai/gpt-4o")
elif os.getenv("OPENAI_API_KEY"):
llm_instance = ChatOpenAI(model="gpt-4o")
except Exception as e:
typer.secho(f'Error initializing LLM: {e}. Would you like to set your OPENAI_API_KEY?', fg=typer.colors.RED)
set_openai_api_key = input('Set OPENAI_API_KEY? (y/n): ')
if set_openai_api_key.lower() == 'y':
os.environ['OPENAI_API_KEY'] = input('Enter your OPENAI_API_KEY: ')
llm_instance = ChatOpenAI(model='gpt-4o')
typer.secho(f"Error initialising LLM: {e}", fg=typer.colors.RED)
raise typer.Exit(code=1)

if llm_instance is None:
typer.secho(
"No LLM API key found. Set OPENROUTER_API_KEY or OPENAI_API_KEY.",
fg=typer.colors.RED,
)
raise typer.Exit(code=1)

builder_service = BuilderService(llm=llm_instance) if llm_instance else None
# recorder_service = RecorderService() # Placeholder
Expand Down
21 changes: 21 additions & 0 deletions workflows/workflow_use/llm/openrouter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import os

from langchain_openai import ChatOpenAI
from pydantic import SecretStr

# Adds support for LLM integration via OpenRouter
class ChatOpenRouter(ChatOpenAI):
def __init__(
self,
model: str,
openai_api_key: str | None = None,
openai_api_base: str = 'https://openrouter.ai/api/v1',
**kwargs,
):
key = openai_api_key
super().__init__(
openai_api_base=openai_api_base,
openai_api_key=SecretStr(key) if key else None,
model_name=model,
**kwargs,
)