Skip to content
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

Get openrouter.ai working with new headers: feature for extra-openai-models.yaml #165

Closed
simonw opened this issue Aug 20, 2023 · 1 comment
Labels
enhancement New feature or request
Milestone

Comments

@simonw
Copy link
Owner

simonw commented Aug 20, 2023

https://openrouter.ai/ runs a paid OpenAI-compatible API for a bunch of models, including Claude 2 (which I have not yet got a real API key for).

It requires a feature that LLM doesn't support yet:

https://openrouter.ai/docs#api-keys

curl https://openrouter.ai/api/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENROUTER_API_KEY" \
-H "HTTP-Referer: $YOUR_SITE_URL" \
-H "X-Title: $YOUR_SITE_NAME" \
-d '{
  "model": "openai/gpt-4-32k", # Optional
  "messages": [
    {"role": "system", "content": "You are a helpful assistant."}, 
    {"role": "user", "content": "Hello!"}
  ]
}'

You have to send two custom HTTP headers.

The OpenAI Python library does support that:

import openai

openai.api_base = "https://openrouter.ai/api/v1"
openai.api_key = OPENROUTER_API_KEY

response = openai.ChatCompletion.create(
    model="openai/gpt-3.5-turbo",
    messages=[...],
    headers={
        "HTTP-Referer": YOUR_SITE_URL,  # To identify your app
        "X-Title": YOUR_APP_NAME,
    },
)
reply = response.choices[0].message
@simonw simonw added the enhancement New feature or request label Aug 20, 2023
@simonw
Copy link
Owner Author

simonw commented Aug 20, 2023

I signed up for an API key at https://openrouter.ai/ and added this to my ~/Library/Application Support/io.datasette.llm/extra-openai-models.yaml file:

- model_id: claude
  model_name: anthropic/claude-2
  api_base: "https://openrouter.ai/api/v1"
  api_key_name: openrouter
  headers:
    HTTP-Referer: "https://llm.datasette.io/"
    X-Title: LLM

Then I ran this:

llm keys set openrouter

And pasted in my key. And now this works:

llm -m claude 'Which model are you?'

I'm Claude, an AI assistant created by Anthropic.

@simonw simonw closed this as completed Aug 20, 2023
@simonw simonw changed the title Get openrouter.ai working Get openrouter.ai working with new headers: feature for extra-openai-models.yaml Aug 20, 2023
@simonw simonw added this to the 0.8 milestone Aug 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant