Files
agent_jrxml/backend/llm.py
T
panda 664de945f1 fix: use raw Anthropic SDK for MiniMax with NO_PROXY workaround
The langchain-anthropic wrapper fails auth with MiniMax because
it sends an api_key that conflicts with ANTHROPIC_AUTH_TOKEN at
the SDK level, causing the request to be sent with incorrect
auth headers. Use raw Anthropic SDK directly with a simple
MiniMaxLLM wrapper class instead.

Root cause: MiniMax requires the API key ONLY via ANTHROPIC_AUTH_TOKEN
(system env), not via api_key parameter or OPENAI_API_KEY. Setting
os.environ["NO_PROXY"]="*" is also needed to prevent httpx from
using a proxy that interferes with the auth header.

Note: E2E testing with streamlit run app.py still pending.
2026-05-15 00:35:41 +08:00

62 lines
2.0 KiB
Python

"""大语言模型工厂:支持 OpenAI 兼容的云端 API、Anthropic 兼容 API 和本地 Ollama。"""
import os
from typing import Any
from dotenv import load_dotenv
load_dotenv()
def get_llm():
backend = os.getenv("LLM_BACKEND", "cloud")
if backend == "local":
from langchain_ollama import ChatOllama
model = os.getenv("LOCAL_LLM_MODEL", "qwen2.5-coder:7b")
return ChatOllama(model=model, temperature=0.1)
provider = os.getenv("LLM_PROVIDER", "openai")
if provider == "anthropic":
from anthropic import Anthropic
api_key = os.getenv("OPENAI_API_KEY", "")
base_url = os.getenv("OPENAI_BASE_URL", "https://api.minimaxi.com/anthropic")
model = os.getenv("LLM_MODEL", "minimax-2.7")
temperature = 0.1
max_tokens = 4096
os.environ["NO_PROXY"] = "*"
client = Anthropic(base_url=base_url, timeout=120)
class MiniMaxLLM:
def invoke(self, prompt: str) -> Any:
resp = client.messages.create(
model=model,
max_tokens=max_tokens,
temperature=temperature,
messages=[{"role": "user", "content": [{"type": "text", "text": prompt}]}],
)
for block in resp.content:
if block.type == "text":
return type("Response", (), {"content": block.text})()
return type("Response", (), {"content": ""})()
def get_num_tokens(self, text: str) -> int:
return client.count_tokens(text)
return MiniMaxLLM()
else:
from langchain_openai import ChatOpenAI
return ChatOpenAI(
model=os.getenv("LLM_MODEL", "gpt-4o"),
api_key=os.getenv("OPENAI_API_KEY"),
base_url=os.getenv("OPENAI_BASE_URL", "https://api.openai.com/v1"),
temperature=0.1,
)
def get_llm_for_correction():
return get_llm()