54 lines
1.7 KiB
Python
54 lines
1.7 KiB
Python
import requests
|
|
|
|
def get_ollama_summary(text, model="granite3.1-moe:1b"):
|
|
url = "http://localhost:11434/v1/chat/completions"
|
|
payload = {
|
|
"model": model,
|
|
"messages": [
|
|
{"role": "system", "content": "You are a helpful assistant that summarizes text."},
|
|
{"role": "user", "content": text}
|
|
]
|
|
}
|
|
response = requests.post(url, json=payload)
|
|
response.raise_for_status()
|
|
return response.json()["choices"][0]["message"]["content"]
|
|
|
|
def get_ollama_embedding(text, model="nomic-embed-text"):
|
|
url = "http://localhost:11434/api/embeddings"
|
|
payload = {
|
|
"model": model,
|
|
"prompt": text
|
|
}
|
|
response = requests.post(url, json=payload)
|
|
response.raise_for_status()
|
|
return response.json()["embedding"]
|
|
|
|
def parse_combined_text_to_dict(s: str) -> dict:
|
|
"""Parse a string of the form 'key1: value1 | key2: value2' into a dict.
|
|
|
|
Rules:
|
|
- Split on ' | ' to get key:value segments.
|
|
- For each segment, split on the first ':' to separate key and value.
|
|
- Strip whitespace. If a value is 'nan' (case-insensitive) or empty, use None.
|
|
- Return a dict mapping keys to values or None.
|
|
"""
|
|
if not s:
|
|
return {}
|
|
result = {}
|
|
parts = [p.strip() for p in s.split("|")]
|
|
for part in parts:
|
|
if not part:
|
|
continue
|
|
# split on the first colon
|
|
if ':' in part:
|
|
k, v = part.split(':', 1)
|
|
k = k.strip()
|
|
v = v.strip()
|
|
if v.lower() == 'nan' or v == '':
|
|
result[k] = None
|
|
else:
|
|
result[k] = v
|
|
else:
|
|
# fallback: store whole segment under a numeric key
|
|
result.setdefault('_extra', []).append(part)
|
|
return result |