Commit ·
eef2ebb
1
Parent(s): 0da76bb
tentative tool call support
Browse filesFiles changed (3)
422 unmodified lines42342442542642742842916 unmodified lines446447448449450422 unmodified linesblock_msg+=f" ({response_obj_call.prompt_feedback.block_reason_message})"raise ValueError(block_msg)print(f"DEBUG: Raw Gemini response_obj_call before conversion: {response_obj_call}") # Kilo Code Added Logif not is_gemini_response_valid(response_obj_call):error_details = f"Invalid non-streaming Gemini response for model string '{model_to_call}'. "if hasattr(response_obj_call, 'candidates'):16 unmodified linesraise ValueError(error_details)openai_response_content = convert_to_openai_format(response_obj_call, request_obj.model)print(f"DEBUG: OpenAI formatted response content before JSONResponse: {openai_response_content}") # Kilo Code Added Logreturn JSONResponse(content=openai_response_content)422 unmodified lines42342442542642742816 unmodified lines445446447448422 unmodified linesblock_msg+=f" ({response_obj_call.prompt_feedback.block_reason_message})"raise ValueError(block_msg)if not is_gemini_response_valid(response_obj_call):error_details = f"Invalid non-streaming Gemini response for model string '{model_to_call}'. "if hasattr(response_obj_call, 'candidates'):16 unmodified linesraise ValueError(error_details)openai_response_content = convert_to_openai_format(response_obj_call, request_obj.model)return JSONResponse(content=openai_response_content)
310 unmodified lines31131231331431531631731831932032132232332432532632732832933033133233333433533633733833931 unmodified lines37137237337437537637737837938038138217 unmodified lines40040140240340440540640740885 unmodified lines49449549649749849950050150250350450510 unmodified lines516517518519520521522310 unmodified linesif gemini_candidate_content and hasattr(gemini_candidate_content, 'parts') and gemini_candidate_content.parts:for part_item in gemini_candidate_content.parts:print(f"DEBUG: Parsing part_item: {part_item}") # Kilo Code Added Logif hasattr(part_item, 'function_call') and part_item.function_call is not None: # Kilo Code: Added 'is not None' checkprint(f"DEBUG: part_item is a function_call, skipping for text parsing.") # Kilo Code Added Logcontinuepart_text = ""if hasattr(part_item, 'text') and part_item.text is not None:part_text = str(part_item.text)# Kilo Code Added Logspart_is_thought = hasattr(part_item, 'thought') and part_item.thought is Trueprint(f"DEBUG: part_text: '{part_text}', is_thought: {part_is_thought}")if part_is_thought:reasoning_text_parts.append(part_text)print(f"DEBUG: Appended to reasoning_text_parts. Current count: {len(reasoning_text_parts)}") # Kilo Code Added Logelif part_text: # Only add if it's not a function_call and has textnormal_text_parts.append(part_text)print(f"DEBUG: Appended to normal_text_parts. Current count: {len(normal_text_parts)}") # Kilo Code Added Logelse:print(f"DEBUG: part_text is empty or not appended. is_thought: {part_is_thought}") # Kilo Code Added Logelif candidate_part_text:print(f"DEBUG: Using candidate_part_text: '{candidate_part_text}'") # Kilo Code Added Lognormal_text_parts.append(candidate_part_text)elif gemini_candidate_content and hasattr(gemini_candidate_content, 'text') and gemini_candidate_content.text is not None:normal_text_parts.append(str(gemini_candidate_content.text))31 unmodified linesfor part in candidate.content.parts:if hasattr(part, 'function_call') and part.function_call is not None: # Kilo Code: Added 'is not None' checkfc = part.function_call# Kilo Code Added Logsprint(f"DEBUG: Processing part with function_call. Part: {part}")print(f"DEBUG: FunctionCall object (fc): {fc}")if fc:print(f"DEBUG: fc.name: {getattr(fc, 'name', 'Name attribute does not exist or is None')}")# End Kilo Code Added Logstool_call_id = f"call_{base_id}_{i}_{fc.name.replace(' ', '_')}_{int(time.time()*10000 + random.randint(0,9999))}"if "tool_calls" not in message_payload:17 unmodified linesreasoning_str = deobfuscate_text(reasoning_str)normal_content_str = deobfuscate_text(normal_content_str)print(f"DEBUG_ASSIGN: normal_content_str before assignment to message_payload: '{normal_content_str}'") # Kilo Code Added Logmessage_payload["content"] = normal_content_strprint(f"DEBUG_ASSIGN: message_payload['content'] after assignment: '{message_payload['content']}'") # Kilo Code Added Logif reasoning_str:message_payload['reasoning_content'] = reasoning_str85 unmodified linesbreakif not function_call_detected_in_chunk:print(f"DEBUG_STREAM: Raw candidate list in chunk for text processing: {candidate}") # Kilo Code Added Log (Note: 'candidate' here is chunk.candidates)if candidate and len(candidate) > 0: # Kilo Code: Ensure candidate list is not emptyreasoning_text, normal_text = parse_gemini_response_for_reasoning_and_content(candidate[0]) # Kilo Code: Pass the first Candidate objectelse:reasoning_text, normal_text = "", "" # Default to empty if no candidatesprint(f"DEBUG_STREAM: Parsed from chunk - reasoning_text: '{reasoning_text}', normal_text: '{normal_text}'") # Kilo Code Added Logif is_encrypt_full:reasoning_text = deobfuscate_text(reasoning_text)normal_text = deobfuscate_text(normal_text)10 unmodified lines# and it's not a terminal chunk, we still send a delta with empty content.delta_payload['content'] = ""print(f"DEBUG_STREAM: Final delta_payload for chunk: {delta_payload}") # Kilo Code Added Logchunk_data = {"id": response_id, "object": "chat.completion.chunk", "created": int(time.time()), "model": model_name,"choices": [{"index": candidate_index, "delta": delta_payload, "finish_reason": openai_finish_reason}]310 unmodified lines31131231331431531631731831932032132232332432532632732832933031 unmodified lines36236336436536636717 unmodified lines38538638738838939039185 unmodified lines47747847948048148248348448548610 unmodified lines497498499500501502310 unmodified linesif gemini_candidate_content and hasattr(gemini_candidate_content, 'parts') and gemini_candidate_content.parts:for part_item in gemini_candidate_content.parts:if hasattr(part_item, 'function_call') and part_item.function_call is not None: # Kilo Code: Added 'is not None' checkcontinuepart_text = ""if hasattr(part_item, 'text') and part_item.text is not None:part_text = str(part_item.text)part_is_thought = hasattr(part_item, 'thought') and part_item.thought is Trueif part_is_thought:reasoning_text_parts.append(part_text)elif part_text: # Only add if it's not a function_call and has textnormal_text_parts.append(part_text)elif candidate_part_text:normal_text_parts.append(candidate_part_text)elif gemini_candidate_content and hasattr(gemini_candidate_content, 'text') and gemini_candidate_content.text is not None:normal_text_parts.append(str(gemini_candidate_content.text))31 unmodified linesfor part in candidate.content.parts:if hasattr(part, 'function_call') and part.function_call is not None: # Kilo Code: Added 'is not None' checkfc = part.function_calltool_call_id = f"call_{base_id}_{i}_{fc.name.replace(' ', '_')}_{int(time.time()*10000 + random.randint(0,9999))}"if "tool_calls" not in message_payload:17 unmodified linesreasoning_str = deobfuscate_text(reasoning_str)normal_content_str = deobfuscate_text(normal_content_str)message_payload["content"] = normal_content_strif reasoning_str:message_payload['reasoning_content'] = reasoning_str85 unmodified linesbreakif not function_call_detected_in_chunk:if candidate and len(candidate) > 0: # Kilo Code: Ensure candidate list is not emptyreasoning_text, normal_text = parse_gemini_response_for_reasoning_and_content(candidate[0]) # Kilo Code: Pass the first Candidate objectelse:reasoning_text, normal_text = "", "" # Default to empty if no candidatesif is_encrypt_full:reasoning_text = deobfuscate_text(reasoning_text)normal_text = deobfuscate_text(normal_text)10 unmodified lines# and it's not a terminal chunk, we still send a delta with empty content.delta_payload['content'] = ""chunk_data = {"id": response_id, "object": "chat.completion.chunk", "created": int(time.time()), "model": model_name,"choices": [{"index": candidate_index, "delta": delta_payload, "finish_reason": openai_finish_reason}]
232 unmodified lines2332342352362372382392402412422432442452462472482492502515 unmodified lines25725825926026126226326426526626726826927027110 unmodified lines28228328428528628728828929029129229329429529629729829930030130230330430530612 unmodified lines319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373232 unmodified linesdel delta['extra_content']content = delta.get('content', '')print(f"DEBUG_OPENAI_STREAM: Chunk {chunk_count} - Original delta content: '{content}'") # Kilo Code Added Logif content:# print(f"DEBUG: Chunk {chunk_count} - Raw content: '{content}'")# Use the processor to extract reasoningprocessed_content, current_reasoning = reasoning_processor.process_chunk(content)print(f"DEBUG_OPENAI_STREAM: Chunk {chunk_count} - Processed by StreamingProcessor: processed_content='{processed_content}', current_reasoning='{current_reasoning}'") # Kilo Code Added Log# Debug logging for processing results# if processed_content or current_reasoning:# print(f"DEBUG: Chunk {chunk_count} - Processed content: '{processed_content}', Reasoning: '{current_reasoning[:50]}...' if len(current_reasoning) > 50 else '{current_reasoning}'")# Send chunks for both reasoning and content as they arrive# Kilo Code: Revised payload constructionoriginal_choice = chunk_as_dict['choices'][0]original_finish_reason = original_choice.get('finish_reason')original_usage = original_choice.get('usage')5 unmodified lines"created": chunk_as_dict["created"], "model": chunk_as_dict["model"],"choices": [{"index": 0, "delta": reasoning_delta, "finish_reason": None}]}yielded_data_str = f"data: {json.dumps(reasoning_payload)}\n\n"print(f"DEBUG_OPENAI_STREAM: Chunk {chunk_count} - Yielding REASONING: {yielded_data_str.strip()}")yield yielded_data_strif processed_content:content_delta = {'content': processed_content}# Determine if this processed_content chunk should carry the original finish_reason and usage.# It should if the reasoning processor is NOT inside a tag after this,# meaning this processed_content is the final part of any tagged content from original_content_from_delta.finish_reason_for_this_content_delta = Noneusage_for_this_content_delta = None10 unmodified linesif usage_for_this_content_delta:content_payload['choices'][0]['usage'] = usage_for_this_content_deltayielded_data_str = f"data: {json.dumps(content_payload)}\n\n"print(f"DEBUG_OPENAI_STREAM: Chunk {chunk_count} - Yielding CONTENT: {yielded_data_str.strip()}")yield yielded_data_strhas_sent_content = True# If neither reasoning nor processed_content was generated from a non-empty original content,# it implies the original content might have been just a tag or something consumed entirely.# In this case, do nothing more for this path; the original chunk's other properties (like finish_reason if content was empty) are handled below.elif original_finish_reason: # Original delta had no content, but had a finish_reason# This case handles chunks that are purely for signaling stream end or other non-content states.yielded_data_str = f"data: {json.dumps(chunk_as_dict)}\n\n" # Yield original chunk as isprint(f"DEBUG_OPENAI_STREAM: Chunk {chunk_count} - Yielding (original delta no content, but has finish_reason): {yielded_data_str.strip()}")yield yielded_data_str# If original delta had no content and no finish_reason, it's an empty delta, yield as is.elif not content and not original_finish_reason : # Kilo Code: Added this conditionyielded_data_str = f"data: {json.dumps(chunk_as_dict)}\n\n"print(f"DEBUG_OPENAI_STREAM: Chunk {chunk_count} - Yielding (original delta empty): {yielded_data_str.strip()}")yield yielded_data_strelse:# Yield chunks without choices too (they might contain metadata)yield f"data: {json.dumps(chunk_as_dict)}\n\n"12 unmodified lines# print(f"DEBUG: Stream ended after {chunk_count} chunks. Buffer state - tag_buffer: '{reasoning_processor.tag_buffer}', "# f"inside_tag: {reasoning_processor.inside_tag}, "# f"reasoning_buffer: '{reasoning_processor.reasoning_buffer[:50]}...' if reasoning_processor.reasoning_buffer else ''")# Flush any remaining buffered contentremaining_content, remaining_reasoning = reasoning_processor.flush_remaining()print(f"DEBUG_OPENAI_STREAM: Flushed from StreamingProcessor: remaining_content='{remaining_content}', remaining_reasoning='{remaining_reasoning}'") # Kilo Code Added Log# Send any remaining reasoning firstif remaining_reasoning:# print(f"DEBUG: Flushing remaining reasoning: '{remaining_reasoning[:50]}...' if len(remaining_reasoning) > 50 else '{remaining_reasoning}'")reasoning_flush_payload = {"id": f"chatcmpl-flush-{int(time.time())}", # Kilo Code: Changed ID for clarity"object": "chat.completion.chunk","created": int(time.time()),"model": request.model,"choices": [{"index": 0, "delta": {"reasoning_content": remaining_reasoning}, "finish_reason": None}]}yielded_data_str = f"data: {json.dumps(reasoning_flush_payload)}\n\n"print(f"DEBUG_OPENAI_STREAM: Yielding from flush (reasoning): {yielded_data_str.strip()}") # Kilo Code Added Logyield yielded_data_str# Send any remaining contentif remaining_content:# print(f"DEBUG: Flushing remaining content: '{remaining_content}'")content_flush_payload = {"id": f"chatcmpl-flush-{int(time.time())}", # Kilo Code: Changed ID for clarity"object": "chat.completion.chunk","created": int(time.time()),"model": request.model,"choices": [{"index": 0, "delta": {"content": remaining_content}, "finish_reason": None}]}yielded_data_str = f"data: {json.dumps(content_flush_payload)}\n\n"print(f"DEBUG_OPENAI_STREAM: Yielding from flush (content): {yielded_data_str.strip()}") # Kilo Code Added Logyield yielded_data_strhas_sent_content = True# Always send a finish reason chunkfinish_payload = {"id": f"chatcmpl-{int(time.time())}","object": "chat.completion.chunk","created": int(time.time()),"model": request.model,"choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}]}yielded_data_str = f"data: {json.dumps(finish_payload)}\n\n"print(f"DEBUG_OPENAI_STREAM: Yielding finish chunk: {yielded_data_str.strip()}") # Kilo Code Added Logyield yielded_data_stryielded_data_str = "data: [DONE]\n\n"print(f"DEBUG_OPENAI_STREAM: Yielding DONE: {yielded_data_str.strip()}") # Kilo Code Added Logyield yielded_data_strexcept Exception as stream_error:error_msg = str(stream_error)232 unmodified lines2332342352362372382392402412422435 unmodified lines24925025125225325425525625725810 unmodified lines26927027127227327427527627727827928028112 unmodified lines294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336232 unmodified linesdel delta['extra_content']content = delta.get('content', '')if content:# Use the processor to extract reasoningprocessed_content, current_reasoning = reasoning_processor.process_chunk(content)# Send chunks for both reasoning and content as they arriveoriginal_choice = chunk_as_dict['choices'][0]original_finish_reason = original_choice.get('finish_reason')original_usage = original_choice.get('usage')5 unmodified lines"created": chunk_as_dict["created"], "model": chunk_as_dict["model"],"choices": [{"index": 0, "delta": reasoning_delta, "finish_reason": None}]}yield f"data: {json.dumps(reasoning_payload)}\n\n"if processed_content:content_delta = {'content': processed_content}finish_reason_for_this_content_delta = Noneusage_for_this_content_delta = None10 unmodified linesif usage_for_this_content_delta:content_payload['choices'][0]['usage'] = usage_for_this_content_deltayield f"data: {json.dumps(content_payload)}\n\n"has_sent_content = Trueelif original_choice.get('finish_reason'): # Check original_choice for finish_reasonyield f"data: {json.dumps(chunk_as_dict)}\n\n"elif not content and not original_choice.get('finish_reason') :yield f"data: {json.dumps(chunk_as_dict)}\n\n"else:# Yield chunks without choices too (they might contain metadata)yield f"data: {json.dumps(chunk_as_dict)}\n\n"12 unmodified lines# print(f"DEBUG: Stream ended after {chunk_count} chunks. Buffer state - tag_buffer: '{reasoning_processor.tag_buffer}', "# f"inside_tag: {reasoning_processor.inside_tag}, "# f"reasoning_buffer: '{reasoning_processor.reasoning_buffer[:50]}...' if reasoning_processor.reasoning_buffer else ''")# Flush any remaining buffered contentremaining_content, remaining_reasoning = reasoning_processor.flush_remaining()# Send any remaining reasoning firstif remaining_reasoning:reasoning_flush_payload = {"id": f"chatcmpl-flush-{int(time.time())}","object": "chat.completion.chunk","created": int(time.time()),"model": request.model,"choices": [{"index": 0, "delta": {"reasoning_content": remaining_reasoning}, "finish_reason": None}]}yield f"data: {json.dumps(reasoning_flush_payload)}\n\n"# Send any remaining contentif remaining_content:content_flush_payload = {"id": f"chatcmpl-flush-{int(time.time())}","object": "chat.completion.chunk","created": int(time.time()),"model": request.model,"choices": [{"index": 0, "delta": {"content": remaining_content}, "finish_reason": None}]}yield f"data: {json.dumps(content_flush_payload)}\n\n"has_sent_content = True# Always send a finish reason chunkfinish_payload = {"id": f"chatcmpl-final-{int(time.time())}", # Kilo Code: Changed ID for clarity"object": "chat.completion.chunk","created": int(time.time()),"model": request.model,"choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}]}yield f"data: {json.dumps(finish_payload)}\n\n"yield "data: [DONE]\n\n"except Exception as stream_error:error_msg = str(stream_error)
app/api_helpers.py-2
app/message_processing.py-20
app/openai_handler.py+13-50