From aca717d047cd74a08ab0c70614f1528498c3bdee Mon Sep 17 00:00:00 2001 From: John Wang Date: Thu, 13 Apr 2023 21:04:10 +0800 Subject: [PATCH] add python sdk of langgenius service-api. --- sdks/python-client/LICENSE | 21 ++++++ sdks/python-client/MANIFEST.in | 1 + sdks/python-client/README.md | 34 +++++++++ .../langgenius_client/__init__.py | 1 + .../python-client/langgenius_client/client.py | 74 +++++++++++++++++++ sdks/python-client/setup.py | 28 +++++++ sdks/python-client/tests/__init__.py | 0 sdks/python-client/tests/test_client.py | 49 ++++++++++++ 8 files changed, 208 insertions(+) create mode 100644 sdks/python-client/LICENSE create mode 100644 sdks/python-client/MANIFEST.in create mode 100644 sdks/python-client/README.md create mode 100644 sdks/python-client/langgenius_client/__init__.py create mode 100644 sdks/python-client/langgenius_client/client.py create mode 100644 sdks/python-client/setup.py create mode 100644 sdks/python-client/tests/__init__.py create mode 100644 sdks/python-client/tests/test_client.py diff --git a/sdks/python-client/LICENSE b/sdks/python-client/LICENSE new file mode 100644 index 0000000000..873e44b4bc --- /dev/null +++ b/sdks/python-client/LICENSE @@ -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. diff --git a/sdks/python-client/MANIFEST.in b/sdks/python-client/MANIFEST.in new file mode 100644 index 0000000000..8b7831626c --- /dev/null +++ b/sdks/python-client/MANIFEST.in @@ -0,0 +1 @@ +recursive-include langgenius_client *.py \ No newline at end of file diff --git a/sdks/python-client/README.md b/sdks/python-client/README.md new file mode 100644 index 0000000000..bdbb9070f0 --- /dev/null +++ b/sdks/python-client/README.md @@ -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) +``` \ No newline at end of file diff --git a/sdks/python-client/langgenius_client/__init__.py b/sdks/python-client/langgenius_client/__init__.py new file mode 100644 index 0000000000..2159d27289 --- /dev/null +++ b/sdks/python-client/langgenius_client/__init__.py @@ -0,0 +1 @@ +from langgenius_client.client import ChatClient, CompletionClient \ No newline at end of file diff --git a/sdks/python-client/langgenius_client/client.py b/sdks/python-client/langgenius_client/client.py new file mode 100644 index 0000000000..dc44a5ed95 --- /dev/null +++ b/sdks/python-client/langgenius_client/client.py @@ -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) diff --git a/sdks/python-client/setup.py b/sdks/python-client/setup.py new file mode 100644 index 0000000000..3c60311660 --- /dev/null +++ b/sdks/python-client/setup.py @@ -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, +) diff --git a/sdks/python-client/tests/__init__.py b/sdks/python-client/tests/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/sdks/python-client/tests/test_client.py b/sdks/python-client/tests/test_client.py new file mode 100644 index 0000000000..45d1204b9b --- /dev/null +++ b/sdks/python-client/tests/test_client.py @@ -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()