-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeepseek.py
74 lines (63 loc) · 2.2 KB
/
deepseek.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Python script for Ollama interaction
import requests
import json
class OllamaClient:
def __init__(self, base_url="http://10.49.34.10:11434"):
self.base_url = base_url
def generate(self, prompt, model="deepseek-r1:32b-qwen-distill-q8_0", stream=False):
"""
Generate a response from the model.
Args:
prompt (str): The input prompt
model (str): Model name to use
stream (bool): Whether to stream the response
"""
if stream:
return self._generate_streaming(prompt, model)
else:
return self._generate_non_streaming(prompt, model)
def _generate_non_streaming(self, prompt, model):
"""Handle non-streaming response."""
url = f"{self.base_url}/api/generate"
payload = {
"model": model,
"prompt": prompt,
"stream": False
}
response = requests.post(url, json=payload)
return response.json()
def _generate_streaming(self, prompt, model):
"""Handle streaming response."""
url = f"{self.base_url}/api/generate"
payload = {
"model": model,
"prompt": prompt,
"stream": True
}
response = requests.post(url, json=payload, stream=True)
for line in response.iter_lines():
if line:
yield json.loads(line)
def list_models(self):
"""List available models."""
url = f"{self.base_url}/api/tags"
response = requests.get(url)
return response.json()
# Example usage
def main():
client = OllamaClient()
# List available models
print("Available models:")
models = client.list_models()
print(json.dumps(models, indent=2))
# Generate response (non-streaming)
print("\nGenerating response:")
response = client.generate("What is the capital of France?")
print(json.dumps(response, indent=2))
# Generate response (streaming)
print("\nStreaming response:")
for chunk in client.generate("Tell me a short story.", stream=True):
if "response" in chunk:
print(chunk["response"], end="")
if __name__ == "__main__":
main()