Files
AI_NovelGenerator/embedding_ollama.py
T

55 lines
1.7 KiB
Python
Raw Normal View History

# embedding_ollama.py
2025-02-02 11:55:49 +08:00
import requests
from typing import List
class OllamaEmbeddings:
"""
Ollama 本地服务提供的 Embedding 接口,
2025-02-02 18:35:10 +08:00
最终拼出形如: http://localhost:11434/api/embed
即 base_url + "/embed"
"""
2025-02-02 11:55:49 +08:00
def __init__(self, model_name: str, base_url: str):
self.model_name = model_name
2025-02-02 18:35:10 +08:00
self.base_url = base_url # 这里一般形如 http://localhost:11434/api (而非 /v1)
2025-02-02 15:50:19 +08:00
2025-02-02 12:59:13 +08:00
def embed(self, texts: List[str]) -> List[List[float]]:
2025-02-02 18:35:10 +08:00
"""
批量将多段文本转换为embedding向量
"""
2025-02-02 12:59:13 +08:00
embeddings = []
for text in texts:
embeddings.append(self.embed_single_document(text))
return embeddings
2025-02-02 11:55:49 +08:00
def embed_documents(self, texts: List[str]) -> List[List[float]]:
"""
2025-02-02 18:35:10 +08:00
兼容langchain的接口写法
2025-02-02 11:55:49 +08:00
"""
2025-02-02 18:35:10 +08:00
return self.embed(texts)
2025-02-02 11:55:49 +08:00
def embed_query(self, query: str) -> List[float]:
"""
将单条 query 转换为 embedding 向量
"""
return self.embed_single_document(query)
def embed_single_document(self, text: str) -> List[float]:
"""
2025-02-02 15:50:19 +08:00
调用 Ollama 本地服务接口,获取文本的 embedding。
2025-02-02 11:55:49 +08:00
"""
2025-02-02 15:50:19 +08:00
url = f"{self.base_url}/embed"
2025-02-02 11:55:49 +08:00
data = {
"model": self.model_name,
"prompt": text
}
try:
response = requests.post(url, json=data)
response.raise_for_status()
result = response.json()
2025-02-02 18:35:10 +08:00
if "embedding" not in result:
raise ValueError("No 'embedding' field in Ollama response.")
2025-02-02 11:55:49 +08:00
return result["embedding"]
except requests.exceptions.RequestException as e:
raise Exception(f"Ollama embeddings request error: {e}")