File size: 1,040 Bytes
7f9dfed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from __future__ import annotations

from collections.abc import Callable
from typing import Any, cast

import requests

from models.response_parsing import chat_completion_payload


def post_json(
    post_func: Callable[..., requests.Response],
    url: str,
    payload: dict[str, Any],
    timeout: float,
    headers: dict[str, str] | None = None,
) -> dict[str, Any]:
    response = post_func(
        url,
        json=payload,
        headers=headers,
        timeout=timeout,
    )
    response.raise_for_status()
    return cast(dict[str, Any], response.json())


def post_chat_completion(
    post_func: Callable[..., requests.Response],
    url: str,
    model: str,
    system_prompt: str,
    user_prompt: str,
    temperature: float,
    max_tokens: int,
    timeout: float,
    headers: dict[str, str] | None = None,
) -> dict[str, Any]:
    return post_json(
        post_func,
        url,
        chat_completion_payload(model, system_prompt, user_prompt, temperature, max_tokens),
        timeout,
        headers,
    )