Open buchikeke opened 1 month ago
I made a custom model for DeepSeek:
import requests
from dsp import LM
class DeepSeek(LM):
def __init__(self, model, api_key, **kwargs,):
self.model = model
self.api_key = api_key
self.provider = "default"
self.history = []
self.kwargs = {
"temperature": kwargs.get("temperature", 0.0),
"max_tokens": min(kwargs.get("max_tokens", 4096), 4096),
"top_p": kwargs.get("top_p", 0.95),
"top_k": kwargs.get("top_k", 1),
"n": kwargs.pop("n", kwargs.pop("num_generations", 1)),
**kwargs,
}
self.kwargs["model"] = model
self.base_url = "https://api.deepseek.com/chat/completions"
def basic_request(self, prompt: str, **kwargs):
headers = {
"Authorization": "Bearer " + self.api_key,
"Accept": "application/json",
"Content-Type": "application/json"
}
data = {
**kwargs,
"model": self.model,
"messages": [
{"role": "user", "content": prompt}
],
"steam": False
}
response = requests.post(self.base_url, headers=headers, json=data)
response = response.json()
self.history.append({
"prompt": prompt,
"response": response,
"kwargs": kwargs,
})
return response
def __call__(self, prompt, **kwargs):
response = self.request(prompt, **kwargs)
completions = [result['message']['content'] for result in response["choices"]]
return completions
Then I could run it by importing the class:
import dspy
import os
from DeepSeek import DeepSeek
lm = DeepSeek(model='deepseek-coder', api_key=os.getenv("DEEPSEEK_PALTFORM_API_KEY"))
dspy.configure(lm=lm)
qa = dspy.ChainOfThought('question -> answer')
response = qa(question="What is the capital of Paris?")
print(response.answer)
"Accept": "application/json",
Thank you ,it works well
我最近有一项dspy的prompt实验,想对比一下不同llm的效果,我不知道该如何接入deepseek,使用官方的OpenAI方法会报错:
上面gpt4方法可以正常调用,deepseek会报下面这个错误:
NotFoundError: Error code: 404 - {'error': {'message': 'Invalid URL (POST /v1chat/completions)', 'type': 'invalid_request_error', 'param': '', 'code': ''}}
具体错误为:请问该怎么解决这个问题?