MCPcopy Create free account
hub / github.com/antirez/llama.cpp-deepseek-v4-flash / chat_completion

Function chat_completion

scripts/server-test-structured.py:98–178  ·  view source on GitHub ↗
(url, messages, tools=None, response_format=None, stream=False)

Source from the content-addressed store, hash-verified

96
97
98def 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:

Callers 2

run_tool_loopFunction · 0.70
run_testFunction · 0.70

Calls 7

jsonMethod · 0.80
print_failFunction · 0.70
print_model_outputFunction · 0.70
postMethod · 0.45
decodeMethod · 0.45
getMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected