MCPcopy
hub / github.com/ModelTC/LightLLM / stream_function_call

Method stream_function_call

test/test_api/test_openai_api.py:136–180  ·  view source on GitHub ↗

stream Function calling

(self, message: str, tools: List[Dict], tool_choice: str = "auto", **kwargs)

Source from the content-addressed store, hash-verified

134 raise Exception(f"API调用失败: {response.status_code} - {response.text}")
135
136 def stream_function_call(self, message: str, tools: List[Dict], tool_choice: str = "auto", **kwargs):
137 """stream Function calling"""
138 data = {
139 "model": self.model_name,
140 "messages": [{"role": "user", "content": message}],
141 "tools": tools,
142 "tool_choice": tool_choice,
143 "stream": True,
144 "temperature": kwargs.get("temperature", 0.7),
145 "max_tokens": kwargs.get("max_tokens", 1000),
146 **kwargs,
147 }
148
149 response = requests.post(f"{self.base_url}/v1/chat/completions", headers=self.headers, json=data, stream=True)
150
151 if response.status_code == 200:
152 content_buffer = ""
153 tool_calls_buffer = []
154
155 for line in response.iter_lines():
156 if line:
157 line = line.decode("utf-8")
158 if line.startswith("data: "):
159 data_str = line[6:]
160 if data_str == "[DONE]":
161 break
162 try:
163 chunk = json.loads(data_str)
164 delta = chunk["choices"][0]["delta"]
165
166 # 处理内容
167 if delta.get("content"):
168 content_buffer += delta["content"]
169 yield {"type": "content", "data": delta["content"]}
170
171 # 处理函数调用
172 if delta.get("tool_calls"):
173 for tool_call in delta["tool_calls"]:
174 tool_calls_buffer.append(tool_call)
175 yield {"type": "tool_call", "data": tool_call}
176
177 except json.JSONDecodeError:
178 continue
179 else:
180 raise Exception(f"API调用失败: {response.status_code} - {response.text}")
181
182 def completions_with_tokens(self, token_ids: List[int], **kwargs) -> Dict[str, Any]:
183 """使用token数组进行文本补全"""

Callers 1

Calls 2

getMethod · 0.45
decodeMethod · 0.45

Tested by

no test coverage detected