DSPy: The framework for programming—not prompting—language models
Python
add sgalng backend
Lianmin Zhengebb5a4b2d5a4
diff --git a/dsp/modules/hf_client.py b/dsp/modules/hf_client.pyindex b36bff4d5..a6cea65df 100644--- a/dsp/modules/hf_client.py+++ b/dsp/modules/hf_client.py@@ -273,3 +273,52 @@ class ChatModuleClient(HFModel):except Exception as e:print("Failed to parse output:", response.text)raise Exception("Received invalid output")+++class HFClientSGLang(HFModel):+ def __init__(self, model, port, url="http://localhost", **kwargs):+ super().__init__(model=model, is_client=True)+ self.url = f"{url}:{port}"+ self.headers = {"Content-Type": "application/json"}++ self.kwargs = {+ "temperature": 0.01,+ "max_tokens": 75,+ "top_p": 0.97,+ "n": 1,+ "stop": ["\n", "\n\n"],+ **kwargs,+ }++ def _generate(self, prompt, **kwargs):+ kwargs = {**self.kwargs, **kwargs}++ payload = {+ "model": kwargs.get("model", "default"),+ "prompt": prompt,+ **kwargs,+ }++ response = send_hfsglang_request_v00(+ f"{self.url}/v1/completions",+ json=payload,+ headers=self.headers,+ )++ try:+ json_response = response.json()+ completions = json_response["choices"]+ response = {+ "prompt": prompt,+ "choices": [{"text": c["text"]} for c in completions],+ }+ return response++ except Exception as e:+ print("Failed to parse JSON response:", response.text)+ raise Exception("Received invalid JSON response from server")+++def send_hfsglang_request_v00(arg, **kwargs):+ return requests.post(arg, **kwargs)diff --git a/dspy/__init__.py b/dspy/__init__.pyindex 0cbfe86b1..d8fb1b36a 100644--- a/dspy/__init__.py+++ b/dspy/__init__.py@@ -1,5 +1,5 @@from dsp.modules.hf_client import ChatModuleClient-from dsp.modules.hf_client import HFServerTGI, HFClientVLLM+from dsp.modules.hf_client import HFServerTGI, HFClientVLLM, HFClientSGLangfrom .signatures import *from .retrieve import *