summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--sqlchat/README.md42
-rw-r--r--sqlchat/__init__.py117
-rw-r--r--testing/sqlchat_test.py7
-rw-r--r--unfinished/sqlchat/README.md3
-rw-r--r--unfinished/sqlchat/__init__.py29
5 files changed, 166 insertions, 32 deletions
diff --git a/sqlchat/README.md b/sqlchat/README.md
new file mode 100644
index 00000000..89d38fae
--- /dev/null
+++ b/sqlchat/README.md
@@ -0,0 +1,42 @@
+### Example: `sqlchat` (use like openai pypi package) <a name="example-sqlchat"></a>
+
+```python
+# Import sqlchat
+import sqlchat
+
+# sqlchat.Completion.create
+# sqlchat.StreamCompletion.create
+
+[...]
+
+```
+
+#### Example Chatbot
+```python
+messages = []
+
+while True:
+ user = input('you: ')
+
+ sqlchat_cmpl = sqlchat.Completion.create(
+ prompt = user,
+ messages = messages
+ )
+
+ print('gpt:', sqlchat_cmpl.completion.choices[0].text)
+
+ messages.extend([
+ {'role': 'user', 'content': user },
+ {'role': 'assistant', 'content': sqlchat_cmpl.completion.choices[0].text}
+ ])
+```
+
+#### Streaming Response:
+
+```python
+for response in sqlchat.StreamCompletion.create(
+ prompt = 'write python code to reverse a string',
+ messages = []):
+
+ print(response.completion.choices[0].text)
+```
diff --git a/sqlchat/__init__.py b/sqlchat/__init__.py
new file mode 100644
index 00000000..b55c0e46
--- /dev/null
+++ b/sqlchat/__init__.py
@@ -0,0 +1,117 @@
+from requests import post
+from time import time
+
+headers = {
+ 'authority' : 'www.sqlchat.ai',
+ 'accept' : '*/*',
+ 'accept-language': 'en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3',
+ 'content-type' : 'text/plain;charset=UTF-8',
+ 'origin' : 'https://www.sqlchat.ai',
+ 'referer' : 'https://www.sqlchat.ai/',
+ 'sec-fetch-dest' : 'empty',
+ 'sec-fetch-mode' : 'cors',
+ 'sec-fetch-site' : 'same-origin',
+ 'user-agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36',
+}
+
+class SqlchatResponse:
+ class Completion:
+ class Choices:
+ def __init__(self, choice: dict) -> None:
+ self.text = choice['text']
+ self.content = self.text.encode()
+ self.index = choice['index']
+ self.logprobs = choice['logprobs']
+ self.finish_reason = choice['finish_reason']
+
+ def __repr__(self) -> str:
+ return f'''<__main__.APIResponse.Completion.Choices(\n text = {self.text.encode()},\n index = {self.index},\n logprobs = {self.logprobs},\n finish_reason = {self.finish_reason})object at 0x1337>'''
+
+ def __init__(self, choices: dict) -> None:
+ self.choices = [self.Choices(choice) for choice in choices]
+
+ class Usage:
+ def __init__(self, usage_dict: dict) -> None:
+ self.prompt_tokens = usage_dict['prompt_chars']
+ self.completion_tokens = usage_dict['completion_chars']
+ self.total_tokens = usage_dict['total_chars']
+
+ def __repr__(self):
+ return f'''<__main__.APIResponse.Usage(\n prompt_tokens = {self.prompt_tokens},\n completion_tokens = {self.completion_tokens},\n total_tokens = {self.total_tokens})object at 0x1337>'''
+
+ def __init__(self, response_dict: dict) -> None:
+
+ self.response_dict = response_dict
+ self.id = response_dict['id']
+ self.object = response_dict['object']
+ self.created = response_dict['created']
+ self.model = response_dict['model']
+ self.completion = self.Completion(response_dict['choices'])
+ self.usage = self.Usage(response_dict['usage'])
+
+ def json(self) -> dict:
+ return self.response_dict
+
+class Completion:
+ def create(
+ prompt: str = 'hello world',
+ messages: list = []) -> SqlchatResponse:
+
+ response = post('https://www.sqlchat.ai/api/chat', headers=headers, stream=True,
+ json = {
+ 'messages': messages,
+ 'openAIApiConfig':{'key':'','endpoint':''}})
+
+ return SqlchatResponse({
+ 'id' : f'cmpl-1337-{int(time())}',
+ 'object' : 'text_completion',
+ 'created': int(time()),
+ 'model' : 'gpt-3.5-turbo',
+ 'choices': [{
+ 'text' : response.text,
+ 'index' : 0,
+ 'logprobs' : None,
+ 'finish_reason' : 'stop'
+ }],
+ 'usage': {
+ 'prompt_chars' : len(prompt),
+ 'completion_chars' : len(response.text),
+ 'total_chars' : len(prompt) + len(response.text)
+ }
+ })
+
+class StreamCompletion:
+ def create(
+ prompt : str = 'hello world',
+ messages: list = []) -> SqlchatResponse:
+
+ messages.append({
+ 'role':'user',
+ 'content':prompt
+ })
+
+ response = post('https://www.sqlchat.ai/api/chat', headers=headers, stream=True,
+ json = {
+ 'messages': messages,
+ 'openAIApiConfig':{'key':'','endpoint':''}})
+
+ for chunk in response.iter_content(chunk_size = 2046):
+ yield SqlchatResponse({
+ 'id' : f'cmpl-1337-{int(time())}',
+ 'object' : 'text_completion',
+ 'created': int(time()),
+ 'model' : 'gpt-3.5-turbo',
+
+ 'choices': [{
+ 'text' : chunk.decode(),
+ 'index' : 0,
+ 'logprobs' : None,
+ 'finish_reason' : 'stop'
+ }],
+
+ 'usage': {
+ 'prompt_chars' : len(prompt),
+ 'completion_chars' : len(chunk.decode()),
+ 'total_chars' : len(prompt) + len(chunk.decode())
+ }
+ }) \ No newline at end of file
diff --git a/testing/sqlchat_test.py b/testing/sqlchat_test.py
new file mode 100644
index 00000000..28e9d2e3
--- /dev/null
+++ b/testing/sqlchat_test.py
@@ -0,0 +1,7 @@
+import sqlchat
+
+for response in sqlchat.StreamCompletion.create(
+ prompt = 'write python code to reverse a string',
+ messages = []):
+
+ print(response.completion.choices[0].text, end='') \ No newline at end of file
diff --git a/unfinished/sqlchat/README.md b/unfinished/sqlchat/README.md
deleted file mode 100644
index ddd5c8f9..00000000
--- a/unfinished/sqlchat/README.md
+++ /dev/null
@@ -1,3 +0,0 @@
-https://www.sqlchat.ai/
-to do:
-- code refractoring \ No newline at end of file
diff --git a/unfinished/sqlchat/__init__.py b/unfinished/sqlchat/__init__.py
deleted file mode 100644
index a7c2c54b..00000000
--- a/unfinished/sqlchat/__init__.py
+++ /dev/null
@@ -1,29 +0,0 @@
-import requests
-
-headers = {
- 'authority': 'www.sqlchat.ai',
- 'accept': '*/*',
- 'accept-language': 'en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3',
- 'content-type': 'text/plain;charset=UTF-8',
- 'origin': 'https://www.sqlchat.ai',
- 'referer': 'https://www.sqlchat.ai/',
- 'sec-fetch-dest': 'empty',
- 'sec-fetch-mode': 'cors',
- 'sec-fetch-site': 'same-origin',
- 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36',
-}
-
-data = {
- 'messages':[
- {'role':'system','content':''},
- {'role':'user','content':'hello world'},
- ],
- 'openAIApiConfig':{
- 'key':'',
- 'endpoint':''
- }
-}
-
-response = requests.post('https://www.sqlchat.ai/api/chat', headers=headers, json=data, stream=True)
-for message in response.iter_content(chunk_size=1024):
- print(message) \ No newline at end of file