Skip to content
This repository was archived by the owner on Sep 23, 2025. It is now read-only.

Commit a8e7b38

Browse files
committed
update
1 parent a328494 commit a8e7b38

File tree

6 files changed

+14
-83
lines changed

6 files changed

+14
-83
lines changed

examples/inference/api_server_simple/query_single.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@
5555
)
5656

5757
args = parser.parse_args()
58-
prompt = "Once upon a time,"
58+
# prompt = "Once upon a time,"
59+
prompt = [
60+
{"role": "user", "content": "Which is bigger, the moon or the sun?"},
61+
]
62+
63+
5964
config: Dict[str, Union[int, float]] = {}
6065
if args.max_new_tokens:
6166
config["max_new_tokens"] = int(args.max_new_tokens)

llm_on_ray/finetune/finetune_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class General(BaseModel):
6464
enable_gradient_checkpointing: bool = False
6565
chat_template: Optional[str] = None
6666
default_chat_template: str = (
67-
"{{ bos_token }}"
67+
"Below is an instruction that describes a task. Write a response that appropriately completes the request."
6868
"{% if messages[0]['role'] == 'system' %}"
6969
"{{ raise_exception('System role not supported') }}"
7070
"{% endif %}"

llm_on_ray/inference/chat_template_process.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
# limitations under the License.
1515
#
1616
from typing import List
17-
1817
from llm_on_ray.inference.api_openai_backend.openai_protocol import ChatMessage
1918

2019

@@ -63,14 +62,12 @@ def _extract_messages(self, messages):
6362
return texts, images
6463

6564
def _prepare_image(self, messages: list):
66-
"""Prepare image from history messages."""
6765
from PIL import Image
6866
import requests
6967
from io import BytesIO
7068
import base64
7169
import re
7270

73-
# prepare images
7471
images: List = []
7572
for msg in messages:
7673
msg = dict(msg)

llm_on_ray/inference/models/gpt2.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,3 @@ model_description:
1515
tokenizer_name_or_path: gpt2
1616
gpt_base_model: true
1717
chat_template: "{% if messages[0]['role'] == 'system' %}{% set loop_messages = messages[1:] %}{% set system_message = messages[0]['content'] %}{% else %}{% set loop_messages = messages %}{% set system_message = false %}{% endif %}{% for message in loop_messages %}{% if (message['role'] == 'user') != (loop.index0 % 2 == 0) %}{{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}{% endif %}{% if message['role'] == 'user' %}{{ message['content'].strip() }}{% elif message['role'] == 'assistant' %}{{ message['content'].strip() }}{% endif %}{% endfor %}"
18-

llm_on_ray/inference/predictor_deployment.py

Lines changed: 5 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -340,31 +340,6 @@ def preprocess_prompts(self, input: Union[str, list], tools=None, tool_choice=No
340340
else:
341341
prompt = self.process_tool.get_prompt(input)
342342
return prompt
343-
else:
344-
if isinstance(input, list) and input and isinstance(input[0], dict):
345-
prompt = self.predictor.tokenizer.apply_chat_template(input, tokenize=False)
346-
elif isinstance(input, list) and input and isinstance(input[0], list):
347-
prompt = [
348-
self.predictor.tokenizer.apply_chat_template(t, tokenize=False)
349-
for t in input
350-
]
351-
elif isinstance(input, list) and input and isinstance(input[0], ChatMessage):
352-
messages = []
353-
for chat_message in input:
354-
message = {"role": chat_message.role, "content": chat_message.content}
355-
messages.append(message)
356-
prompt = self.predictor.tokenizer.apply_chat_template(
357-
messages, tokenize=False
358-
)
359-
elif isinstance(input, list) and input and isinstance(input[0], str):
360-
prompt = input
361-
elif isinstance(input, str):
362-
prompt = input
363-
else:
364-
raise TypeError(
365-
f"Unsupported type {type(input)} for text. Expected dict or list of dicts."
366-
)
367-
return prompt
368343
elif prompt_format == PromptFormat.PROMPTS_FORMAT:
369344
raise HTTPException(400, "Invalid prompt format.")
370345
return input
@@ -411,63 +386,18 @@ async def openai_call(
411386
tool_choice=None,
412387
):
413388
self.use_openai = True
389+
print("openai_call")
390+
print(input)
391+
print(type(input))
414392

415393
# return prompt or list of prompts preprocessed
416394
prompts = self.preprocess_prompts(input, tools, tool_choice)
395+
print(prompts)
396+
print(type(prompts))
417397

418398
# Handle streaming response
419399
if streaming_response:
420400
async for result in self.handle_streaming(prompts, config):
421401
yield result
422402
else:
423403
yield await self.handle_non_streaming(prompts, config)
424-
425-
def _extract_messages(self, messages):
426-
texts, images = [], []
427-
for message in messages:
428-
if message["role"] == "user" and isinstance(message["content"], list):
429-
texts.append({"role": "user", "content": message["content"][0]["text"]})
430-
images.append(
431-
{"role": "user", "content": message["content"][1]["image_url"]["url"]}
432-
)
433-
else:
434-
texts.append(message)
435-
return texts, images
436-
437-
def _prepare_image(self, messages: list):
438-
"""Prepare image from history messages."""
439-
from PIL import Image
440-
import requests
441-
from io import BytesIO
442-
import base64
443-
import re
444-
445-
# prepare images
446-
images: List = []
447-
if isinstance(messages[0], List):
448-
for i in range(len(messages)):
449-
for msg in messages[i]:
450-
msg = dict(msg)
451-
content = msg["content"]
452-
if "url" not in content:
453-
continue
454-
is_data = len(re.findall("^data:image/.+;base64,", content["url"])) > 0
455-
if is_data:
456-
encoded_str = re.sub("^data:image/.+;base64,", "", content["url"])
457-
images[i].append(Image.open(BytesIO(base64.b64decode(encoded_str))))
458-
else:
459-
images[i].append(Image.open(requests.get(content["url"], stream=True).raw))
460-
elif isinstance(messages[0], dict):
461-
for msg in messages:
462-
msg = dict(msg)
463-
content = msg["content"]
464-
if "url" not in content:
465-
continue
466-
is_data = len(re.findall("^data:image/.+;base64,", content["url"])) > 0
467-
if is_data:
468-
encoded_str = re.sub("^data:image/.+;base64,", "", content["url"])
469-
images.append(Image.open(BytesIO(base64.b64decode(encoded_str))))
470-
else:
471-
images.append(Image.open(requests.get(content["url"], stream=True).raw))
472-
473-
return images

llm_on_ray/inference/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,13 @@ def is_cpu_without_ipex(infer_conf: InferenceConfig) -> bool:
162162
return (not infer_conf.ipex.enabled) and infer_conf.device == DEVICE_CPU
163163

164164

165-
def get_prompt_format(input: Union[List[str], List[dict], List[List[dict]], List[ChatMessage]]):
165+
def get_prompt_format(input: Union[List[str], List[dict], List[ChatMessage]]):
166166
chat_format = True
167167
prompts_format = True
168168
for item in input:
169169
if isinstance(item, str):
170170
chat_format = False
171-
elif isinstance(item, dict) or isinstance(item, ChatMessage) or isinstance(item, list):
171+
elif isinstance(item, dict) or isinstance(item, ChatMessage):
172172
prompts_format = False
173173
else:
174174
chat_format = False

0 commit comments

Comments
 (0)