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

Method stream_chat

test/test_api/test_openai_api.py:41–68  ·  view source on GitHub ↗
(self, message: str, **kwargs)

Source from the content-addressed store, hash-verified

39 raise Exception(f"API调用失败: {response.status_code} - {response.text}")
40
41 def stream_chat(self, message: str, **kwargs):
42 data = {
43 "model": self.model_name,
44 "messages": [{"role": "user", "content": message}],
45 "stream": True,
46 "temperature": kwargs.get("temperature", 0.7),
47 "max_tokens": kwargs.get("max_tokens", 1000),
48 **kwargs,
49 }
50
51 response = requests.post(f"{self.base_url}/v1/chat/completions", headers=self.headers, json=data, stream=True)
52
53 if response.status_code == 200:
54 for line in response.iter_lines():
55 if line:
56 line = line.decode("utf-8")
57 if line.startswith("data: "):
58 data_str = line[6:]
59 if data_str == "[DONE]":
60 break
61 try:
62 chunk = json.loads(data_str)
63 if chunk["choices"][0]["delta"].get("content"):
64 yield chunk["choices"][0]["delta"]["content"]
65 except json.JSONDecodeError:
66 continue
67 else:
68 raise Exception(f"API调用失败: {response.status_code} - {response.text}")
69
70 def completions(self, prompt: str, **kwargs) -> Dict[str, Any]:
71 """文本补全"""

Callers 1

test_stream_chatFunction · 0.95

Calls 2

getMethod · 0.45
decodeMethod · 0.45

Tested by

no test coverage detected