add python sdk of langgenius service-api.
parent
2e4af43f65
commit
aca717d047
@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2023 LangGenius
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
@ -0,0 +1 @@
|
|||||||
|
recursive-include langgenius_client *.py
|
||||||
@ -0,0 +1,34 @@
|
|||||||
|
# langgenius-client
|
||||||
|
A LangGenius App Service-API Client, using for build a webapp by request Service-API
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```
|
||||||
|
api_key = "your_api_key"
|
||||||
|
|
||||||
|
# Initialize CompletionClient
|
||||||
|
completion_client = CompletionClient(api_key)
|
||||||
|
|
||||||
|
# Create Completion Message using CompletionClient
|
||||||
|
completion_response = completion_client.create_completion_message(inputs={}, query="Hello", response_mode="blocking", user="user_id")
|
||||||
|
print(completion_response)
|
||||||
|
|
||||||
|
# Initialize ChatClient
|
||||||
|
chat_client = ChatClient(api_key)
|
||||||
|
|
||||||
|
# Create Chat Message using ChatClient
|
||||||
|
chat_response = chat_client.create_chat_message(inputs={}, message="Hello", user="user_id", response_mode="streaming")
|
||||||
|
print(chat_response)
|
||||||
|
|
||||||
|
# Get Chat History using ChatClient
|
||||||
|
chat_history = chat_client.get_chat_history(user="user_id")
|
||||||
|
print(chat_history)
|
||||||
|
|
||||||
|
# Get Conversation List using ChatClient
|
||||||
|
conversations = chat_client.list_conversations(user="user_id")
|
||||||
|
print(conversations)
|
||||||
|
|
||||||
|
# Rename Conversation using ChatClient
|
||||||
|
rename_conversation_response = chat_client.rename_conversation(conversation_id="conversation_id", name="new_name", user="user_id")
|
||||||
|
print(rename_conversation_response)
|
||||||
|
```
|
||||||
@ -0,0 +1 @@
|
|||||||
|
from langgenius_client.client import ChatClient, CompletionClient
|
||||||
@ -0,0 +1,74 @@
|
|||||||
|
import requests
|
||||||
|
|
||||||
|
|
||||||
|
class LangGeniusClient:
|
||||||
|
def __init__(self, api_key):
|
||||||
|
self.api_key = api_key
|
||||||
|
self.base_url = "https://api.langgenius.ai/v1"
|
||||||
|
|
||||||
|
def _send_request(self, method, endpoint, data=None, params=None, stream=False):
|
||||||
|
headers = {
|
||||||
|
"Authorization": f"Bearer {self.api_key}",
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
}
|
||||||
|
|
||||||
|
url = f"{self.base_url}{endpoint}"
|
||||||
|
response = requests.request(method, url, json=data, params=params, headers=headers, stream=stream)
|
||||||
|
|
||||||
|
return response
|
||||||
|
|
||||||
|
def message_feedback(self, message_id, rating, user):
|
||||||
|
data = {
|
||||||
|
"rating": rating,
|
||||||
|
"user": user
|
||||||
|
}
|
||||||
|
return self._send_request("POST", f"/messages/{message_id}/feedbacks", data)
|
||||||
|
|
||||||
|
def get_application_parameters(self, user):
|
||||||
|
params = {"user": user}
|
||||||
|
return self._send_request("GET", "/parameters", params=params)
|
||||||
|
|
||||||
|
|
||||||
|
class CompletionClient(LangGeniusClient):
|
||||||
|
def create_completion_message(self, inputs, query, response_mode, user):
|
||||||
|
data = {
|
||||||
|
"inputs": inputs,
|
||||||
|
"query": query,
|
||||||
|
"response_mode": response_mode,
|
||||||
|
"user": user
|
||||||
|
}
|
||||||
|
return self._send_request("POST", "/completion-messages", data, stream=True if response_mode == "streaming" else False)
|
||||||
|
|
||||||
|
|
||||||
|
class ChatClient(LangGeniusClient):
|
||||||
|
def create_chat_message(self, inputs, query, user, response_mode="blocking", conversation_id=None):
|
||||||
|
data = {
|
||||||
|
"inputs": inputs,
|
||||||
|
"query": query,
|
||||||
|
"user": user,
|
||||||
|
"response_mode": response_mode
|
||||||
|
}
|
||||||
|
if conversation_id:
|
||||||
|
data["conversation_id"] = conversation_id
|
||||||
|
|
||||||
|
return self._send_request("POST", "/chat-messages", data, stream=True if response_mode == "streaming" else False)
|
||||||
|
|
||||||
|
def get_conversation_messages(self, user, conversation_id=None, first_id=None, limit=None):
|
||||||
|
params = {"user": user}
|
||||||
|
|
||||||
|
if conversation_id:
|
||||||
|
params["conversation_id"] = conversation_id
|
||||||
|
if first_id:
|
||||||
|
params["first_id"] = first_id
|
||||||
|
if limit:
|
||||||
|
params["limit"] = limit
|
||||||
|
|
||||||
|
return self._send_request("GET", "/messages", params=params)
|
||||||
|
|
||||||
|
def get_conversations(self, user, first_id=None, limit=None, pinned=None):
|
||||||
|
params = {"user": user, "first_id": first_id, "limit": limit, "pinned": pinned}
|
||||||
|
return self._send_request("GET", "/conversations", params=params)
|
||||||
|
|
||||||
|
def rename_conversation(self, conversation_id, name, user):
|
||||||
|
data = {"name": name, "user": user}
|
||||||
|
return self._send_request("PATCH", f"/conversations/{conversation_id}", data)
|
||||||
@ -0,0 +1,28 @@
|
|||||||
|
from setuptools import setup
|
||||||
|
|
||||||
|
with open("README.md", "r", encoding="utf-8") as fh:
|
||||||
|
long_description = fh.read()
|
||||||
|
|
||||||
|
setup(
|
||||||
|
name="langgenius-client",
|
||||||
|
version="0.1.3",
|
||||||
|
author="LangGenius",
|
||||||
|
author_email="hello@langgenius.ai",
|
||||||
|
description="A package for interacting with the LangGenius Service-API",
|
||||||
|
long_description=long_description,
|
||||||
|
long_description_content_type="text/markdown",
|
||||||
|
url="https://github.com/langgenius/langgenius-client",
|
||||||
|
license='MIT',
|
||||||
|
packages=['langgenius'],
|
||||||
|
classifiers=[
|
||||||
|
"Programming Language :: Python :: 3",
|
||||||
|
"License :: OSI Approved :: MIT License",
|
||||||
|
"Operating System :: OS Independent",
|
||||||
|
],
|
||||||
|
python_requires=">=3.6",
|
||||||
|
install_requires=[
|
||||||
|
"requests"
|
||||||
|
],
|
||||||
|
keywords='langgenius nlp ai language-processing',
|
||||||
|
include_package_data=True,
|
||||||
|
)
|
||||||
@ -0,0 +1,49 @@
|
|||||||
|
import os
|
||||||
|
import unittest
|
||||||
|
from langgenius.client import ChatClient, CompletionClient, LangGeniusClient
|
||||||
|
|
||||||
|
API_KEY = os.environ.get("API_KEY")
|
||||||
|
APP_ID = os.environ.get("APP_ID")
|
||||||
|
|
||||||
|
|
||||||
|
class TestChatClient(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.chat_client = ChatClient(API_KEY)
|
||||||
|
|
||||||
|
def test_create_chat_message(self):
|
||||||
|
response = self.chat_client.create_chat_message({}, "Hello, World!", "test_user")
|
||||||
|
self.assertIn("message_id", response)
|
||||||
|
|
||||||
|
def test_get_conversation_messages(self):
|
||||||
|
response = self.chat_client.get_conversation_messages("test_user")
|
||||||
|
self.assertIsInstance(response, list)
|
||||||
|
|
||||||
|
def test_get_conversations(self):
|
||||||
|
response = self.chat_client.get_conversations("test_user")
|
||||||
|
self.assertIsInstance(response, list)
|
||||||
|
|
||||||
|
|
||||||
|
class TestCompletionClient(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.completion_client = CompletionClient(API_KEY)
|
||||||
|
|
||||||
|
def test_create_completion_message(self):
|
||||||
|
response = self.completion_client.create_completion_message({}, "What's the weather like today?", "blocking", "test_user")
|
||||||
|
self.assertIn("message_id", response)
|
||||||
|
|
||||||
|
|
||||||
|
class TestLangGeniusClient(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.langgenius_client = LangGeniusClient(API_KEY)
|
||||||
|
|
||||||
|
def test_message_feedback(self):
|
||||||
|
response = self.langgenius_client.message_feedback("test_message_id", 5, "test_user")
|
||||||
|
self.assertIn("success", response)
|
||||||
|
|
||||||
|
def test_get_application_parameters(self):
|
||||||
|
response = self.langgenius_client.get_application_parameters("test_user")
|
||||||
|
self.assertIsInstance(response, dict)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
Loading…
Reference in New Issue