mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-08 20:42:30 +08:00
refactor some llm api using openai api format (#1692)
### What problem does this PR solve? refactor some llm api using openai api format ### Type of change - [x] Refactoring --------- Co-authored-by: Zhedong Cen <cenzhedong2@126.com>
This commit is contained in:
@ -378,7 +378,7 @@ class OllamaCV(Base):
|
||||
def chat(self, system, history, gen_conf, image=""):
|
||||
if system:
|
||||
history[-1]["content"] = system + history[-1]["content"] + "user query: " + history[-1]["content"]
|
||||
|
||||
|
||||
try:
|
||||
for his in history:
|
||||
if his["role"] == "user":
|
||||
@ -433,27 +433,16 @@ class OllamaCV(Base):
|
||||
yield 0
|
||||
|
||||
|
||||
class LocalAICV(Base):
|
||||
class LocalAICV(GptV4):
|
||||
def __init__(self, key, model_name, base_url, lang="Chinese"):
|
||||
if not base_url:
|
||||
raise ValueError("Local cv model url cannot be None")
|
||||
if base_url.split("/")[-1] != "v1":
|
||||
base_url = os.path.join(base_url, "v1")
|
||||
self.client = OpenAI(api_key="empty", base_url=base_url)
|
||||
self.model_name = model_name.split("___")[0]
|
||||
self.lang = lang
|
||||
|
||||
def describe(self, image, max_tokens=300):
|
||||
b64 = self.image2base64(image)
|
||||
prompt = self.prompt(b64)
|
||||
for i in range(len(prompt)):
|
||||
for c in prompt[i]["content"]:
|
||||
if "text" in c:
|
||||
c["type"] = "text"
|
||||
|
||||
res = self.client.chat.completions.create(
|
||||
model=self.model_name,
|
||||
messages=prompt,
|
||||
max_tokens=max_tokens,
|
||||
)
|
||||
return res.choices[0].message.content.strip(), res.usage.total_tokens
|
||||
|
||||
|
||||
class XinferenceCV(Base):
|
||||
def __init__(self, key, model_name="", lang="Chinese", base_url=""):
|
||||
@ -549,60 +538,19 @@ class GeminiCV(Base):
|
||||
yield response._chunks[-1].usage_metadata.total_token_count
|
||||
|
||||
|
||||
class OpenRouterCV(Base):
|
||||
class OpenRouterCV(GptV4):
|
||||
def __init__(
|
||||
self,
|
||||
key,
|
||||
model_name,
|
||||
lang="Chinese",
|
||||
base_url="https://openrouter.ai/api/v1/chat/completions",
|
||||
base_url="https://openrouter.ai/api/v1",
|
||||
):
|
||||
if not base_url:
|
||||
base_url = "https://openrouter.ai/api/v1"
|
||||
self.client = OpenAI(api_key=key, base_url=base_url)
|
||||
self.model_name = model_name
|
||||
self.lang = lang
|
||||
self.base_url = "https://openrouter.ai/api/v1/chat/completions"
|
||||
self.key = key
|
||||
|
||||
def describe(self, image, max_tokens=300):
|
||||
b64 = self.image2base64(image)
|
||||
response = requests.post(
|
||||
url=self.base_url,
|
||||
headers={
|
||||
"Authorization": f"Bearer {self.key}",
|
||||
},
|
||||
data=json.dumps(
|
||||
{
|
||||
"model": self.model_name,
|
||||
"messages": self.prompt(b64),
|
||||
"max_tokens": max_tokens,
|
||||
}
|
||||
),
|
||||
)
|
||||
response = response.json()
|
||||
return (
|
||||
response["choices"][0]["message"]["content"].strip(),
|
||||
response["usage"]["total_tokens"],
|
||||
)
|
||||
|
||||
def prompt(self, b64):
|
||||
return [
|
||||
{
|
||||
"role": "user",
|
||||
"content": [
|
||||
{
|
||||
"type": "image_url",
|
||||
"image_url": {"url": f"data:image/jpeg;base64,{b64}"},
|
||||
},
|
||||
{
|
||||
"type": "text",
|
||||
"text": (
|
||||
"请用中文详细描述一下图中的内容,比如时间,地点,人物,事情,人物心情等,如果有数据请提取出数据。"
|
||||
if self.lang.lower() == "chinese"
|
||||
else "Please describe the content of this picture, like where, when, who, what happen. If it has number data, please extract them out."
|
||||
),
|
||||
},
|
||||
],
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
class LocalCV(Base):
|
||||
@ -675,12 +623,12 @@ class NvidiaCV(Base):
|
||||
]
|
||||
|
||||
|
||||
class LmStudioCV(LocalAICV):
|
||||
class LmStudioCV(GptV4):
|
||||
def __init__(self, key, model_name, base_url, lang="Chinese"):
|
||||
if not base_url:
|
||||
raise ValueError("Local llm url cannot be None")
|
||||
if base_url.split('/')[-1] != 'v1':
|
||||
self.base_url = os.path.join(base_url,'v1')
|
||||
self.client = OpenAI(api_key="lm-studio", base_url=self.base_url)
|
||||
if base_url.split("/")[-1] != "v1":
|
||||
base_url = os.path.join(base_url, "v1")
|
||||
self.client = OpenAI(api_key="lm-studio", base_url=base_url)
|
||||
self.model_name = model_name
|
||||
self.lang = lang
|
||||
|
||||
Reference in New Issue
Block a user