add support for LM Studio (#1663)

### What problem does this PR solve?

#1602 

### 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:
黄腾
2024-07-24 12:46:43 +08:00
committed by GitHub
parent 100b3165d8
commit d96348eb22
10 changed files with 9791 additions and 26 deletions

View File

@ -440,15 +440,8 @@ class LocalAICV(Base):
self.lang = lang
def describe(self, image, max_tokens=300):
if not isinstance(image, bytes) and not isinstance(
image, BytesIO
): # if url string
prompt = self.prompt(image)
for i in range(len(prompt)):
prompt[i]["content"]["image_url"]["url"] = image
else:
b64 = self.image2base64(image)
prompt = self.prompt(b64)
b64 = self.image2base64(image)
prompt = self.prompt(b64)
for i in range(len(prompt)):
for c in prompt[i]["content"]:
if "text" in c:
@ -680,3 +673,14 @@ class NvidiaCV(Base):
"content": text + f' <img src="data:image/jpeg;base64,{b64}"/>',
}
]
class LmStudioCV(LocalAICV):
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)
self.model_name = model_name
self.lang = lang