mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-08 20:42:30 +08:00
'load llm infomation from a json file and add support for OpenRouter' (#1533)
### What problem does this PR solve? #1467 ### Type of change - [x] New Feature (non-breaking change which adds functionality) --------- Co-authored-by: Zhedong Cen <cenzhedong2@126.com>
This commit is contained in:
@ -23,6 +23,8 @@ from openai import OpenAI
|
||||
import os
|
||||
import base64
|
||||
from io import BytesIO
|
||||
import json
|
||||
import requests
|
||||
|
||||
from api.utils import get_uuid
|
||||
from api.utils.file_utils import get_project_base_directory
|
||||
@ -212,7 +214,7 @@ class GeminiCV(Base):
|
||||
self.model = GenerativeModel(model_name=self.model_name)
|
||||
self.model._client = _client
|
||||
self.lang = lang
|
||||
|
||||
|
||||
def describe(self, image, max_tokens=2048):
|
||||
from PIL.Image import open
|
||||
gen_config = {'max_output_tokens':max_tokens}
|
||||
@ -227,6 +229,63 @@ class GeminiCV(Base):
|
||||
)
|
||||
return res.text,res.usage_metadata.total_token_count
|
||||
|
||||
|
||||
class OpenRouterCV(Base):
|
||||
def __init__(
|
||||
self,
|
||||
key,
|
||||
model_name,
|
||||
lang="Chinese",
|
||||
base_url="https://openrouter.ai/api/v1/chat/completions",
|
||||
):
|
||||
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):
|
||||
def __init__(self, key, model_name="glm-4v", lang="Chinese", **kwargs):
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user