-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathollama_client.py
More file actions
48 lines (44 loc) · 1.7 KB
/
ollama_client.py
File metadata and controls
48 lines (44 loc) · 1.7 KB
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
import os
import requests
from typing import Optional
OLLAMA_HOST = os.environ.get('OLLAMA_HOST', 'http://localhost:11434')
MODEL = os.environ.get('OLLAMA_MODEL', 'deepseek-coder') # set a model you have locally
def ollama_convert_prompt(source_code: str, language: str = 'java') -> str:
prompt = f"""
You are a helpful assistant that converts a Java or C# DTO/entity into a TypeScript interface.
Rules:
- Remove all Java/C# annotations or attributes.
- Preserve field names.
- Map primitive types to TypeScript primitives (numbers, boolean, string).
- Convert List/Array/IEnumerable to arrays (T[]).
- If field annotated with @NotNull or [Required] mark required (no ?), otherwise mark optional (?).
- For unknown types leave the type name as-is.
- Output only TypeScript code, no extra explanation.
Input ({language}):
{source_code}
"""
return prompt
def ollama_request(prompt: str, model: Optional[str] = None, timeout: int = 60) -> Optional[str]:
if model is None:
model = MODEL
url = f"{OLLAMA_HOST}/api/chat"
payload = {
"model": model,
"messages": [
{"role": "user", "content": prompt}
],
"temperature": 0
}
try:
resp = requests.post(url, json=payload, timeout=timeout)
resp.raise_for_status()
data = resp.json()
# Ollama returns a stream sometimes; try to extract text
# If API returns 'choices' with 'message' -> typical Chat API shape
if 'choices' in data and data['choices']:
return data['choices'][0]['message']['content']
# fallback: whole response as text
return resp.text
except Exception as e:
print("Ollama request failed:", e)
return None