-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
70 lines (64 loc) · 2.23 KB
/
utils.py
File metadata and controls
70 lines (64 loc) · 2.23 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import numpy as np
import requests
import json
class HuggingFaceClient:
def __init__(self, hf_token: str, model: str):
self.base_url = "https://router.huggingface.co/v1"
self.hf_token = hf_token
self.model = model
def generate_embedding(self, texts: str) -> np.ndarray:
api_url = f"https://router.huggingface.co/hf-inference/models/{self.model}/pipeline/feature-extraction"
headers = {"Authorization": f"Bearer {self.hf_token}"}
response = requests.post(
api_url,
headers=headers,
json={"inputs": texts, "options": {"wait_for_model": True}},
)
if response.status_code == 200:
return response.json()
"""
ollama_url = "http://localhost:11434/api/embed"
headers = {"Content-Type": "application/json"}
data = {"model": "embeddinggemma", "input": text}
response = requests.post(
ollama_url,
data=json.dumps(data),
headers=headers,
)
if response.status_code == 200:
embedding = response.json().get("embeddings", [])
return embedding[0]
else:
print(
"Failed to generate embedding:",
response.status_code,
response.text,
)
return np.zeros(768) # Return a zero vector on failure
"""
class OpenRouterClient:
def __init__(self, model: str, api_key: str):
self.model = model
self.api_key = api_key
self.base_url = "https://openrouter.ai/api/v1"
def chat(self, prompt: str) -> str:
response = requests.post(
url="https://openrouter.ai/api/v1/chat/completions",
headers={
"Authorization": f"Bearer {self.api_key}",
},
data=json.dumps(
{
"model": self.model,
"messages": [
{
"role": "user",
"content": prompt,
}
],
}
),
)
response.raise_for_status()
j = response.json()["choices"][0]["message"]["content"]
return j