-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringableinference.py
More file actions
124 lines (103 loc) · 3.73 KB
/
stringableinference.py
File metadata and controls
124 lines (103 loc) · 3.73 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from typing import Optional
import requests
import base64
import json
class StringableInference:
"""
A client for interacting with the StringableInference API.
Attributes:
base_url (str): Base URL of the API.
model (str): Model identifier to use for completions.
session (requests.Session): Session object for making HTTP requests.
"""
def __init__(self, model: str) -> None:
"""
Initialize the client with a specific model.
Args:
model (str): The model identifier to use for API requests.
(you can get available models using the get_models method)
"""
self.base_url = "https://stringableinf.com/api"
self.model = model
self.session = requests.Session()
def chat_completion(
self,
messages: list,
stream: Optional[bool] = False,
http_referer: Optional[str] = "https://bhattaraisusan.com.np",
) -> dict:
"""
Send a chat completion request to the API.
Args:
messages (list): A list of message dictionaries to send.
stream (Optional[bool]): Whether to enable streaming responses. Default is False.
http_referer (Optional[str]): The HTTP referer header value. Default is a preset URL.
Returns:
dict: The JSON response from the API.
Raises:
Exception: If the HTTP request fails or the server returns an error.
"""
try:
response = self.session.post(
self.base_url + "/v1/chat/completions",
headers={
"Content-Type": "application/json",
"HTTP-Referer": http_referer,
"X-Title": "AI Assistant",
},
json={
"model": self.model,
"messages": messages,
"stream": stream,
},
stream=stream,
timeout=15
)
response.raise_for_status()
return json.loads(response.content)
except Exception as e:
print("Request failed:", e)
raise
def get_models(self) -> list:
"""
Retrieve the list of available models from the API.
Returns:
list: A list of model backend IDs.
Raises:
Exception: If the HTTP request fails or the server returns an error.
"""
try:
response = self.session.get(self.base_url + "/models", timeout=15)
response.raise_for_status()
models_json = json.loads(response.content)
models_list = [
model["backend_id"] for model in models_json["models"]
]
return models_list
except Exception as e:
print("Request failed:", e)
raise
def chat_completion_with_images(self) -> None:
"""
Placeholder for chat completion with image support.
Currently not supported by the provider. Intended to send messages
containing images for processing.
"""
# Not yet supported by the provider
# with open("image.jpg", "rb") as f:
# image_data = base64.b64encode(f.read()).decode()
# messages = [
# {
# "role": "user",
# "content": [
# {"type": "text", "text": "Describe this image"},
# {
# "type": "image_url",
# "image_url": {
# "url": f"data:image/jpeg;base64,{image_data}"
# },
# },
# ],
# }
# ]
pass