mirror of
https://github.com/BoardWare-Genius/jarvis-models.git
synced 2025-12-13 16:53:24 +00:00
feat: support user_context
This commit is contained in:
@ -27,7 +27,6 @@ def is_base64(value) -> bool:
|
|||||||
except Exception:
|
except Exception:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@singleton
|
|
||||||
@singleton
|
@singleton
|
||||||
class VLMS(Blackbox):
|
class VLMS(Blackbox):
|
||||||
|
|
||||||
@ -133,27 +132,27 @@ class VLMS(Blackbox):
|
|||||||
|
|
||||||
# 'https://raw.githubusercontent.com/open-mmlab/mmdeploy/main/tests/data/tiger.jpeg'
|
# 'https://raw.githubusercontent.com/open-mmlab/mmdeploy/main/tests/data/tiger.jpeg'
|
||||||
## Lmdeploy
|
## Lmdeploy
|
||||||
if not user_context:
|
# if not user_context:
|
||||||
user_context = []
|
# user_context = []
|
||||||
|
|
||||||
## Predefine user_context only for testing
|
## Predefine user_context only for testing
|
||||||
# user_context = [{'role':'user','content':'你好,我叫康康,你是谁?'}, {'role': 'assistant', 'content': '你好!很高兴为你提供帮助。'}]
|
# user_context = [{'role':'user','content':'你好,我叫康康,你是谁?'}, {'role': 'assistant', 'content': '你好!很高兴为你提供帮助。'}]
|
||||||
user_context = [{
|
# user_context = [{
|
||||||
'role': 'user',
|
# 'role': 'user',
|
||||||
'content': [{
|
# 'content': [{
|
||||||
'type': 'text',
|
# 'type': 'text',
|
||||||
'text': '图中有什么,请描述一下',
|
# 'text': '图中有什么,请描述一下',
|
||||||
}, {
|
# }, {
|
||||||
'type': 'image_url',
|
# 'type': 'image_url',
|
||||||
'image_url': {
|
# 'image_url': {
|
||||||
'url': 'https://raw.githubusercontent.com/open-mmlab/mmdeploy/main/tests/data/tiger.jpeg'
|
# 'url': 'https://raw.githubusercontent.com/open-mmlab/mmdeploy/main/tests/data/tiger.jpeg'
|
||||||
},
|
# },
|
||||||
}]
|
# }]
|
||||||
},{
|
# },{
|
||||||
'role': 'assistant',
|
# 'role': 'assistant',
|
||||||
'content': '图片中主要展示了一只老虎,它正在绿色的草地上休息。草地上有很多可以让人坐下的地方,而且看起来相当茂盛。背景比较模糊,可能是因为老虎的影响,让整个图片的其他部分都变得不太清晰了。'
|
# 'content': '图片中主要展示了一只老虎,它正在绿色的草地上休息。草地上有很多可以让人坐下的地方,而且看起来相当茂盛。背景比较模糊,可能是因为老虎的影响,让整个图片的其他部分都变得不太清晰了。'
|
||||||
}
|
# }
|
||||||
]
|
# ]
|
||||||
api_client = APIClient(self.url)
|
api_client = APIClient(self.url)
|
||||||
model_name = api_client.available_models[0]
|
model_name = api_client.available_models[0]
|
||||||
|
|
||||||
@ -208,9 +207,62 @@ class VLMS(Blackbox):
|
|||||||
user_context = messages + [{'role': 'assistant', 'content': responses}]
|
user_context = messages + [{'role': 'assistant', 'content': responses}]
|
||||||
return responses, user_context
|
return responses, user_context
|
||||||
|
|
||||||
|
def _into_openai_format(self, context:List[list]):
|
||||||
|
"""
|
||||||
|
Convert the data into openai format.
|
||||||
|
context: a list of list, each element have the form [user_input, response],
|
||||||
|
and the first one of list 'user_input' is also tuple with [,text]; [image,text] or [[imgs],text]
|
||||||
|
#TODO: add support for multiple images
|
||||||
|
"""
|
||||||
|
user_context = []
|
||||||
|
for i,item in enumerate(context):
|
||||||
|
user_content = item[0]
|
||||||
|
if isinstance(user_content, list):
|
||||||
|
if len(user_content) == 1:
|
||||||
|
user_content = [{
|
||||||
|
'type': 'text',
|
||||||
|
'text': user_content[0]
|
||||||
|
}]
|
||||||
|
elif is_base64(user_content[0]):
|
||||||
|
user_content = [{
|
||||||
|
'type': 'image_url',
|
||||||
|
'image_url': {
|
||||||
|
'url': f"data:image/jpeg;base64,{user_content[0]}"
|
||||||
|
},
|
||||||
|
},{
|
||||||
|
'type': 'text',
|
||||||
|
'text': user_content[1]
|
||||||
|
}]
|
||||||
|
else:
|
||||||
|
user_content = [{
|
||||||
|
'type': 'image_url',
|
||||||
|
'image_url': {
|
||||||
|
'url': user_content[0]
|
||||||
|
},
|
||||||
|
},{
|
||||||
|
'type': 'text',
|
||||||
|
'text': user_content[1]
|
||||||
|
}]
|
||||||
|
else:
|
||||||
|
user_content = [{
|
||||||
|
'type': 'text',
|
||||||
|
'text': user_content
|
||||||
|
}]
|
||||||
|
user_context.append({
|
||||||
|
'role': 'user',
|
||||||
|
'content': user_content
|
||||||
|
})
|
||||||
|
|
||||||
|
user_context.append({
|
||||||
|
'role': 'assistant',
|
||||||
|
'content': item[1]
|
||||||
|
})
|
||||||
|
|
||||||
|
return user_context
|
||||||
|
|
||||||
|
|
||||||
async def fast_api_handler(self, request: Request) -> Response:
|
async def fast_api_handler(self, request: Request) -> Response:
|
||||||
|
## TODO: add support for multiple images and support image in form-data format
|
||||||
json_request = True
|
json_request = True
|
||||||
try:
|
try:
|
||||||
content_type = request.headers['content-type']
|
content_type = request.headers['content-type']
|
||||||
@ -225,7 +277,13 @@ class VLMS(Blackbox):
|
|||||||
model_name = data.get("model_name")
|
model_name = data.get("model_name")
|
||||||
prompt = data.get("prompt")
|
prompt = data.get("prompt")
|
||||||
settings: dict = data.get('settings')
|
settings: dict = data.get('settings')
|
||||||
|
context = data.get("context")
|
||||||
|
print(context)
|
||||||
|
print(type(context))
|
||||||
|
|
||||||
|
user_context = self._into_openai_format(context) if context else []
|
||||||
|
|
||||||
|
print(user_context)
|
||||||
if json_request:
|
if json_request:
|
||||||
img_data = data.get("img_data")
|
img_data = data.get("img_data")
|
||||||
else:
|
else:
|
||||||
@ -238,7 +296,7 @@ class VLMS(Blackbox):
|
|||||||
if model_name is None or model_name.isspace():
|
if model_name is None or model_name.isspace():
|
||||||
model_name = "Qwen-VL-Chat"
|
model_name = "Qwen-VL-Chat"
|
||||||
|
|
||||||
response, history = self.processing(prompt, img_data,settings, model_name)
|
response, history = self.processing(prompt, img_data,settings, model_name,user_context=user_context)
|
||||||
# jsonresp = str(JSONResponse(content={"response": self.processing(prompt, img_data, model_name)}).body, "utf-8")
|
# jsonresp = str(JSONResponse(content={"response": self.processing(prompt, img_data, model_name)}).body, "utf-8")
|
||||||
|
|
||||||
return JSONResponse(content={"response": response, "history": history}, status_code=status.HTTP_200_OK)
|
return JSONResponse(content={"response": response, "history": history}, status_code=status.HTTP_200_OK)
|
||||||
Reference in New Issue
Block a user