(
request: ResponseCreateRequest,
raw_request: Request,
api_key: str = Depends(verify_api_key),
tmp_dir: Path = Depends(get_temp_dir),
image_store: Path = Depends(get_image_store_dir),
)
| 1936 | |
| 1937 | @router.post("/v1/responses") |
| 1938 | async def create_response( |
| 1939 | request: ResponseCreateRequest, |
| 1940 | raw_request: Request, |
| 1941 | api_key: str = Depends(verify_api_key), |
| 1942 | tmp_dir: Path = Depends(get_temp_dir), |
| 1943 | image_store: Path = Depends(get_image_store_dir), |
| 1944 | ): |
| 1945 | base_url = str(raw_request.base_url) |
| 1946 | base_messages, norm_input = _response_items_to_messages(request.input) |
| 1947 | struct_req = _build_structured_requirement(request.response_format) |
| 1948 | extra_instr = [struct_req.instruction] if struct_req else [] |
| 1949 | |
| 1950 | standard_tools, image_tools = [], [] |
| 1951 | if request.tools: |
| 1952 | for t in request.tools: |
| 1953 | if isinstance(t, Tool): |
| 1954 | standard_tools.append(t) |
| 1955 | elif isinstance(t, ResponseImageTool): |
| 1956 | image_tools.append(t) |
| 1957 | elif isinstance(t, dict): |
| 1958 | if t.get("type") == "function": |
| 1959 | standard_tools.append(Tool.model_validate(t)) |
| 1960 | elif t.get("type") == "image_generation": |
| 1961 | image_tools.append(ResponseImageTool.model_validate(t)) |
| 1962 | |
| 1963 | img_instr = _build_image_generation_instruction( |
| 1964 | image_tools, |
| 1965 | request.tool_choice if isinstance(request.tool_choice, ResponseToolChoice) else None, |
| 1966 | ) |
| 1967 | if img_instr: |
| 1968 | extra_instr.append(img_instr) |
| 1969 | preface = _instructions_to_messages(request.instructions) |
| 1970 | conv_messages = [*preface, *base_messages] if preface else base_messages |
| 1971 | model_tool_choice = ( |
| 1972 | request.tool_choice if isinstance(request.tool_choice, (str, ToolChoiceFunction)) else None |
| 1973 | ) |
| 1974 | |
| 1975 | messages = _prepare_messages_for_model( |
| 1976 | conv_messages, |
| 1977 | standard_tools or None, |
| 1978 | model_tool_choice, |
| 1979 | extra_instr or None, |
| 1980 | ) |
| 1981 | pool, db = GeminiClientPool(), LMDBConversationStore() |
| 1982 | try: |
| 1983 | model = _get_model_by_name(request.model) |
| 1984 | except ValueError as exc: |
| 1985 | raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc)) from exc |
| 1986 | |
| 1987 | session, client, remain = await _find_reusable_session(db, pool, model, messages) |
| 1988 | reused_session = session is not None |
| 1989 | use_google_temporary_mode = g_config.gemini.chat_mode == ChatMode.TEMPORARY |
| 1990 | if session: |
| 1991 | msgs = _prepare_messages_for_model( |
| 1992 | remain, |
| 1993 | request.tools, |
| 1994 | request.tool_choice, |
| 1995 | None, |
nothing calls this directly
no test coverage detected