(url, messages, tools=None, response_format=None, stream=False)
| 96 | |
| 97 | |
| 98 | def chat_completion(url, messages, tools=None, response_format=None, stream=False): |
| 99 | payload = { |
| 100 | "messages": messages, |
| 101 | "stream": stream, |
| 102 | "max_tokens": 4096, |
| 103 | } |
| 104 | if tools: |
| 105 | payload["tools"] = tools |
| 106 | payload["tool_choice"] = "auto" |
| 107 | if response_format is not None: |
| 108 | payload["response_format"] = response_format |
| 109 | |
| 110 | try: |
| 111 | response = requests.post(url, json=payload, stream=stream) |
| 112 | response.raise_for_status() |
| 113 | except requests.exceptions.RequestException as e: |
| 114 | body = e.response.content if (e.response is not None) else b"" |
| 115 | print_fail(f"Request error: {e} | body: {body}") |
| 116 | return None |
| 117 | |
| 118 | full_content = "" |
| 119 | reasoning_content = "" |
| 120 | tool_calls: list[dict] = [] |
| 121 | |
| 122 | if stream: |
| 123 | for line in response.iter_lines(): |
| 124 | if not line: |
| 125 | continue |
| 126 | decoded = line.decode("utf-8") |
| 127 | if not decoded.startswith("data: "): |
| 128 | continue |
| 129 | data_str = decoded[6:] |
| 130 | if data_str == "[DONE]": |
| 131 | break |
| 132 | try: |
| 133 | data = json.loads(data_str) |
| 134 | except json.JSONDecodeError: |
| 135 | continue |
| 136 | choices = data.get("choices", []) |
| 137 | if not choices: |
| 138 | continue |
| 139 | delta = choices[0].get("delta", {}) |
| 140 | if delta.get("reasoning_content"): |
| 141 | reasoning_content += delta["reasoning_content"] |
| 142 | if delta.get("content"): |
| 143 | full_content += delta["content"] |
| 144 | print_model_output(delta["content"]) |
| 145 | for tc in delta.get("tool_calls", []): |
| 146 | idx = tc.get("index", 0) |
| 147 | while len(tool_calls) <= idx: |
| 148 | tool_calls.append( |
| 149 | { |
| 150 | "id": "", |
| 151 | "type": "function", |
| 152 | "function": {"name": "", "arguments": ""}, |
| 153 | } |
| 154 | ) |
| 155 | if "id" in tc: |
no test coverage detected