Skip to content

Commit

Permalink
fix: fixed openrouter provider, added Referer/X-Title header for open…
Browse files Browse the repository at this point in the history
…router requests
  • Loading branch information
ErikBjare committed Aug 21, 2024
1 parent 6a58900 commit c50bfda
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions gptme/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def reply(messages: list[Message], model: str, stream: bool = False) -> Message:

def _chat_complete(messages: list[Message], model: str) -> str:
provider = _client_to_provider()
if provider == "openai":
if provider in ["openai", "azure", "openrouter"]:
return chat_openai(messages, model)
elif provider == "anthropic":
return chat_anthropic(messages, model)
Expand All @@ -64,7 +64,7 @@ def _chat_complete(messages: list[Message], model: str) -> str:

def _stream(messages: list[Message], model: str) -> Iterator[str]:
provider = _client_to_provider()
if provider == "openai":
if provider in ["openai", "azure", "openrouter"]:
return stream_openai(messages, model)
elif provider == "anthropic":
return stream_anthropic(messages, model)
Expand Down
13 changes: 13 additions & 0 deletions gptme/llm_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
logger = logging.getLogger(__name__)


# Shows in rankings on openrouter.ai
openrouter_headers = {
"HTTP-Referer": "https://github.com/ErikBjare/gptme",
"X-Title": "gptme",
}


def init(llm: str, config):
global openai

Expand Down Expand Up @@ -50,6 +57,9 @@ def chat(messages: list[Message], model: str) -> str:
messages=msgs2dicts(messages, openai=True), # type: ignore
temperature=TEMPERATURE,
top_p=TOP_P,
extra_headers=(
openrouter_headers if "openrouter.ai" in str(openai.base_url) else {}
),
)
content = response.choices[0].message.content
assert content
Expand All @@ -68,6 +78,9 @@ def stream(messages: list[Message], model: str) -> Generator[str, None, None]:
# the llama-cpp-python server needs this explicitly set, otherwise unreliable results
# TODO: make this better
max_tokens=1000 if not model.startswith("gpt-") else 4096,
extra_headers=(
openrouter_headers if "openrouter.ai" in str(openai.base_url) else {}
),
):
if not chunk.choices: # type: ignore
# Got a chunk with no choices, Azure always sends one of these at the start
Expand Down

0 comments on commit c50bfda

Please sign in to comment.