Commit dedfeb3
Changed files (3)
src
src/llm/hooks.py
@@ -1,13 +1,16 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from config import GPT
-from llm.prompts import refine_prompts
+from llm.prompts import modify_prompts, refine_prompts
from utils import unicode_to_ascii
def pre_hooks(client: dict, completions: dict, message_info: dict | None = None):
pre_openrouter_hook(client, completions)
pre_helicone_hook(client, message_info)
+ # Gemini tends to respond in English, even when the user's query is in another language.
+ if GPT.GEMINI_PREFER_LANG and "gemini" in completions["model"].lower():
+ modify_prompts(completions["messages"], prompt=f"请使用{GPT.GEMINI_PREFER_LANG}回复。", role="system", method="append")
completions["messages"] = refine_prompts(completions["messages"])
src/llm/prompts.py
@@ -15,7 +15,7 @@ def modify_prompts(context: list[dict], prompt: str, role: str = "system", metho
return context
if not context:
return [{"role": role, "content": prompt}]
- if method not in ["overwrite", "prepend", "append"]:
+ if method not in ["overwrite", "append"]:
logger.warning(f"Invalid method of `modify_prompts`: {method}")
return context
if method == "overwrite":
@@ -23,10 +23,16 @@ def modify_prompts(context: list[dict], prompt: str, role: str = "system", metho
context[0]["content"] = prompt
else:
context.insert(0, {"role": role, "content": prompt})
- elif method == "prepend":
- context.insert(0, {"role": role, "content": prompt})
elif method == "append":
- context.append({"role": role, "content": prompt})
+ if role == "system": # should in the beginning
+ pos = 0
+ for idx, item in enumerate(context):
+ if item["role"] != "system":
+ pos = idx
+ break
+ context.insert(pos, {"role": role, "content": prompt})
+ else:
+ context.append({"role": role, "content": prompt})
return context
src/config.py
@@ -184,6 +184,7 @@ class GPT: # see `llm/README.md`
GEMINI_API_KEY = os.getenv("GPT_GEMINI_API_KEY", "")
GEMINI_BASE_URL = os.getenv("GPT_GEMINI_BASE_URL", "https://generativelanguage.googleapis.com/v1beta/openai")
GEMINI_IMAGE_CAPABILITY = os.getenv("GPT_GEMINI_IMAGE_CAPABILITY", "1").lower() in ["1", "y", "yes", "t", "true", "on"]
+ GEMINI_PREFER_LANG = os.getenv("GPT_GEMINI_PREFER_LANG", "") # Set a prefer response language for Gemini
# /gpt command
OPENAI_MODEL = os.getenv("GPT_OPENAI_MODEL", "gpt-4o")
OPENAI_MODEL_NAME = os.getenv("GPT_OPENAI_MODEL_NAME", "GPT-4o")