Spaces:
Sleeping
Sleeping
Update LaasApiClient class to include the chat_completions method for making API calls to the chat completions endpoint. This allows users to interact with the chat completions API and receive responses. Also, remove the unused 'hash' parameter from the call_llm_preset method.
Browse files- libs/laas.py +33 -5
libs/laas.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
import os
|
2 |
-
from typing import Any, Dict, Optional
|
3 |
|
4 |
import requests
|
5 |
|
@@ -11,7 +11,6 @@ class LaasApiClient:
|
|
11 |
Usage:
|
12 |
client = LaasApiClient()
|
13 |
response = client.call_llm_preset(
|
14 |
-
hash="your_hash",
|
15 |
params={},
|
16 |
model="your_model",
|
17 |
messages=[],
|
@@ -22,6 +21,10 @@ class LaasApiClient:
|
|
22 |
source_count=0
|
23 |
)
|
24 |
print(response)
|
|
|
|
|
|
|
|
|
25 |
"""
|
26 |
|
27 |
BASE_URL = "https://api-laas.wanted.co.kr"
|
@@ -31,22 +34,28 @@ class LaasApiClient:
|
|
31 |
base_url: str = BASE_URL,
|
32 |
api_key: Optional[str] = None,
|
33 |
project: Optional[str] = None,
|
|
|
34 |
):
|
35 |
self.base_url = base_url
|
|
|
36 |
self.headers = {
|
37 |
"Content-Type": "application/json",
|
38 |
"apiKey": api_key or os.environ.get("LAAS_API_KEY"),
|
39 |
"project": project or os.environ.get("LAAS_PROJECT"),
|
40 |
}
|
41 |
if not self.headers["apiKey"]:
|
42 |
-
raise ValueError("API key is required")
|
43 |
if not self.headers["project"]:
|
44 |
-
raise ValueError("Project is required")
|
|
|
|
|
45 |
|
46 |
def _make_api_call(self, endpoint: str, payload: Dict[str, Any]) -> Dict[str, Any]:
|
47 |
"""Make an API call to the specified endpoint with the given payload."""
|
48 |
url = f"{self.base_url}/api/{endpoint}"
|
49 |
-
response = requests.post(
|
|
|
|
|
50 |
response.raise_for_status() # Raise an exception for HTTP errors
|
51 |
return response.json()
|
52 |
|
@@ -63,3 +72,22 @@ class LaasApiClient:
|
|
63 |
"""
|
64 |
endpoint = "preset/complete" if complete else "preset"
|
65 |
return self._make_api_call(endpoint, kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
from typing import Any, Dict, List, Optional
|
3 |
|
4 |
import requests
|
5 |
|
|
|
11 |
Usage:
|
12 |
client = LaasApiClient()
|
13 |
response = client.call_llm_preset(
|
|
|
14 |
params={},
|
15 |
model="your_model",
|
16 |
messages=[],
|
|
|
21 |
source_count=0
|
22 |
)
|
23 |
print(response)
|
24 |
+
|
25 |
+
# For chat completions:
|
26 |
+
chat_response = client.chat_completions(messages=[{"role": "user", "content": "Hello!"}])
|
27 |
+
print(chat_response)
|
28 |
"""
|
29 |
|
30 |
BASE_URL = "https://api-laas.wanted.co.kr"
|
|
|
34 |
base_url: str = BASE_URL,
|
35 |
api_key: Optional[str] = None,
|
36 |
project: Optional[str] = None,
|
37 |
+
hash: Optional[str] = None,
|
38 |
):
|
39 |
self.base_url = base_url
|
40 |
+
self.hash = hash or os.environ.get("LAAS_HASH")
|
41 |
self.headers = {
|
42 |
"Content-Type": "application/json",
|
43 |
"apiKey": api_key or os.environ.get("LAAS_API_KEY"),
|
44 |
"project": project or os.environ.get("LAAS_PROJECT"),
|
45 |
}
|
46 |
if not self.headers["apiKey"]:
|
47 |
+
raise ValueError("API key is required to use the LAAS API.")
|
48 |
if not self.headers["project"]:
|
49 |
+
raise ValueError("Project is required to use the LAAS API.")
|
50 |
+
if not self.hash:
|
51 |
+
raise ValueError("Hash is required to use the LAAS API.")
|
52 |
|
53 |
def _make_api_call(self, endpoint: str, payload: Dict[str, Any]) -> Dict[str, Any]:
|
54 |
"""Make an API call to the specified endpoint with the given payload."""
|
55 |
url = f"{self.base_url}/api/{endpoint}"
|
56 |
+
response = requests.post(
|
57 |
+
url, headers=self.headers, json={"hash": self.hash, **payload}
|
58 |
+
)
|
59 |
response.raise_for_status() # Raise an exception for HTTP errors
|
60 |
return response.json()
|
61 |
|
|
|
72 |
"""
|
73 |
endpoint = "preset/complete" if complete else "preset"
|
74 |
return self._make_api_call(endpoint, kwargs)
|
75 |
+
|
76 |
+
def chat_completions(
|
77 |
+
self, messages: List[Dict[str, str]], **kwargs
|
78 |
+
) -> Dict[str, Any]:
|
79 |
+
"""
|
80 |
+
Call the chat completions API.
|
81 |
+
|
82 |
+
Args:
|
83 |
+
messages (List[Dict[str, str]]): List of message dictionaries.
|
84 |
+
**kwargs: Additional keyword arguments for the API call.
|
85 |
+
|
86 |
+
Returns:
|
87 |
+
Dict[str, Any]: The JSON response from the API.
|
88 |
+
"""
|
89 |
+
payload = {
|
90 |
+
"messages": messages,
|
91 |
+
**kwargs,
|
92 |
+
}
|
93 |
+
return self._make_api_call("preset/chat/completions", payload)
|