(request: ChatCompletionRequest, raw_request: Request)
| 64 | |
| 65 | |
| 66 | async def chat_completions_impl(request: ChatCompletionRequest, raw_request: Request) -> Response: |
| 67 | from .api_http import g_objs |
| 68 | |
| 69 | if request.logit_bias is not None: |
| 70 | return create_error_response( |
| 71 | HTTPStatus.BAD_REQUEST, |
| 72 | "The logit_bias parameter is not currently supported", |
| 73 | ) |
| 74 | |
| 75 | if request.function_call != "none": |
| 76 | return create_error_response(HTTPStatus.BAD_REQUEST, "The function call feature is not supported") |
| 77 | |
| 78 | created_time = int(time.time()) |
| 79 | |
| 80 | multimodal_params_dict = {"images": []} |
| 81 | for message in request.messages: |
| 82 | if isinstance(message.content, list): |
| 83 | texts = [] |
| 84 | for content in message.content: |
| 85 | if content.type == "text" and content.text: |
| 86 | texts.append(content.text) |
| 87 | elif content.type == "image_url" and content.image_url is not None: |
| 88 | img = content.image_url.url |
| 89 | if img.startswith("http://") or img.startswith("https://"): |
| 90 | multimodal_params_dict["images"].append({"type": "url", "data": img}) |
| 91 | elif img.startswith("data:image"): |
| 92 | # "data:image/jpeg;base64,{base64_image}" |
| 93 | data_str = img.split(";", 1)[1] |
| 94 | if data_str.startswith("base64,"): |
| 95 | data = data_str[7:] |
| 96 | multimodal_params_dict["images"].append({"type": "base64", "data": data}) |
| 97 | else: |
| 98 | raise ValueError("Unrecognized image input.") |
| 99 | else: |
| 100 | raise ValueError( |
| 101 | "Unrecognized image input. Supports local path, http url, base64, and PIL.Image." |
| 102 | ) |
| 103 | |
| 104 | tools = None |
| 105 | if request.tools and request.tool_choice != "none": |
| 106 | # request.skip_special_tokens = False |
| 107 | if not isinstance(request.tool_choice, str): |
| 108 | tools = [ |
| 109 | item.function.model_dump() |
| 110 | for item in request.tools |
| 111 | if item.function.name == request.tool_choice.function.name |
| 112 | ] |
| 113 | else: |
| 114 | tools = [item.function.model_dump() for item in request.tools] |
| 115 | |
| 116 | prompt = await build_prompt(request, tools) |
| 117 | sampling_params_dict = { |
| 118 | "do_sample": True, |
| 119 | "presence_penalty": request.presence_penalty, |
| 120 | "frequency_penalty": request.frequency_penalty, |
| 121 | "repetition_penalty": request.repetition_penalty, |
| 122 | "temperature": request.temperature, |
| 123 | "top_p": request.top_p, |
no test coverage detected