Skip to content

How to use OpenRouter API for DeepSeek? #567

Description

@agn-7

Bug Description

I'm trying to use DeepSeek model on Browser-use, however, I don't have a direct API key from Deepseek, instead, I have an API key from Open Router that supports all LLM models. Now, based on the Open Router API doc and the browser-use doc regarding customizing the LLM model I have reached the following code. However, the browser-use opens up a browser instance, but nothing happens and the page remains empty!

Reproduction Steps

Here are the corresponding logs:

INFO     [browser_use] BrowserUse logging setup complete with level info
INFO     [root] Anonymized telemetry enabled. See https://docs.browser-use.com/development/telemetry for more information.
INFO     [agent] 🚀 Starting task: 1. Go to https://www.reddit.com/r/LocalLLaMA 2. Search for 'browser use' in the search bar3. Click on first result4. Return the first comment
INFO     [agent] 📍 Step 1
ERROR    [agent] ❌ Result failed 1/3 times:
 Could not parse response.
INFO     [agent] 📍 Step 1
ERROR    [agent] ❌ Result failed 2/3 times:
 {'message': 'Provider returned error', 'code': 429, 'metadata': {'raw': '{\n  "error_msg": "Authentication Fails. Multiple 401 errors detected. Please wait for 1 minute before trying again."\n}', 'provider_name': 'DeepSeek'}}
INFO     [agent] 📍 Step 1
ERROR    [agent] ❌ Result failed 3/3 times:
 {'message': 'Provider returned error', 'code': 429, 'metadata': {'raw': '{\n  "error_msg": "Authentication Fails. Multiple 401 errors detected. Please wait for 1 minute before trying again."\n}', 'provider_name': 'DeepSeek'}}
ERROR    [agent] ❌ Stopping due to 3 consecutive failures
INFO     [agent] Created GIF at agent_history.gif

Code Sample

Here is the code:

import asyncio
import os

from dotenv import load_dotenv
from langchain_openai import ChatOpenAI

from browser_use import Agent

# dotenv
load_dotenv()

api_key = "sk-or-v1-xxx"
if not api_key:
	raise ValueError('DEEPSEEK_API_KEY is not set')


async def run_search():
	agent = Agent(
		task=(
			'1. Go to https://www.reddit.com/r/LocalLLaMA '
			"2. Search for 'browser use' in the search bar"
			'3. Click on first result'
			'4. Return the first comment'
		),
		llm=ChatOpenAI(
			base_url='https://openrouter.ai/api/v1',
			model='deepseek/deepseek-chat',
			api_key=api_key,
		),
		use_vision=False,
	)

	await agent.run()


if __name__ == '__main__':
	asyncio.run(run_search())

Version

0.1.35

LLM Model

DeepSeek Coder

Operating System

Ubuntu 22.04

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions