t.me/xtekky
commited on
Commit
•
85a1961
1
Parent(s):
121976b
sqlchat
Browse files- sqlchat/README.md +42 -0
- sqlchat/__init__.py +117 -0
- testing/sqlchat_test.py +7 -0
- unfinished/sqlchat/README.md +0 -3
- unfinished/sqlchat/__init__.py +0 -29
sqlchat/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
### Example: `sqlchat` (use like openai pypi package) <a name="example-sqlchat"></a>
|
2 |
+
|
3 |
+
```python
|
4 |
+
# Import sqlchat
|
5 |
+
import sqlchat
|
6 |
+
|
7 |
+
# sqlchat.Completion.create
|
8 |
+
# sqlchat.StreamCompletion.create
|
9 |
+
|
10 |
+
[...]
|
11 |
+
|
12 |
+
```
|
13 |
+
|
14 |
+
#### Example Chatbot
|
15 |
+
```python
|
16 |
+
messages = []
|
17 |
+
|
18 |
+
while True:
|
19 |
+
user = input('you: ')
|
20 |
+
|
21 |
+
sqlchat_cmpl = sqlchat.Completion.create(
|
22 |
+
prompt = user,
|
23 |
+
messages = messages
|
24 |
+
)
|
25 |
+
|
26 |
+
print('gpt:', sqlchat_cmpl.completion.choices[0].text)
|
27 |
+
|
28 |
+
messages.extend([
|
29 |
+
{'role': 'user', 'content': user },
|
30 |
+
{'role': 'assistant', 'content': sqlchat_cmpl.completion.choices[0].text}
|
31 |
+
])
|
32 |
+
```
|
33 |
+
|
34 |
+
#### Streaming Response:
|
35 |
+
|
36 |
+
```python
|
37 |
+
for response in sqlchat.StreamCompletion.create(
|
38 |
+
prompt = 'write python code to reverse a string',
|
39 |
+
messages = []):
|
40 |
+
|
41 |
+
print(response.completion.choices[0].text)
|
42 |
+
```
|
sqlchat/__init__.py
ADDED
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from requests import post
|
2 |
+
from time import time
|
3 |
+
|
4 |
+
headers = {
|
5 |
+
'authority' : 'www.sqlchat.ai',
|
6 |
+
'accept' : '*/*',
|
7 |
+
'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',
|
8 |
+
'content-type' : 'text/plain;charset=UTF-8',
|
9 |
+
'origin' : 'https://www.sqlchat.ai',
|
10 |
+
'referer' : 'https://www.sqlchat.ai/',
|
11 |
+
'sec-fetch-dest' : 'empty',
|
12 |
+
'sec-fetch-mode' : 'cors',
|
13 |
+
'sec-fetch-site' : 'same-origin',
|
14 |
+
'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',
|
15 |
+
}
|
16 |
+
|
17 |
+
class SqlchatResponse:
|
18 |
+
class Completion:
|
19 |
+
class Choices:
|
20 |
+
def __init__(self, choice: dict) -> None:
|
21 |
+
self.text = choice['text']
|
22 |
+
self.content = self.text.encode()
|
23 |
+
self.index = choice['index']
|
24 |
+
self.logprobs = choice['logprobs']
|
25 |
+
self.finish_reason = choice['finish_reason']
|
26 |
+
|
27 |
+
def __repr__(self) -> str:
|
28 |
+
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>'''
|
29 |
+
|
30 |
+
def __init__(self, choices: dict) -> None:
|
31 |
+
self.choices = [self.Choices(choice) for choice in choices]
|
32 |
+
|
33 |
+
class Usage:
|
34 |
+
def __init__(self, usage_dict: dict) -> None:
|
35 |
+
self.prompt_tokens = usage_dict['prompt_chars']
|
36 |
+
self.completion_tokens = usage_dict['completion_chars']
|
37 |
+
self.total_tokens = usage_dict['total_chars']
|
38 |
+
|
39 |
+
def __repr__(self):
|
40 |
+
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>'''
|
41 |
+
|
42 |
+
def __init__(self, response_dict: dict) -> None:
|
43 |
+
|
44 |
+
self.response_dict = response_dict
|
45 |
+
self.id = response_dict['id']
|
46 |
+
self.object = response_dict['object']
|
47 |
+
self.created = response_dict['created']
|
48 |
+
self.model = response_dict['model']
|
49 |
+
self.completion = self.Completion(response_dict['choices'])
|
50 |
+
self.usage = self.Usage(response_dict['usage'])
|
51 |
+
|
52 |
+
def json(self) -> dict:
|
53 |
+
return self.response_dict
|
54 |
+
|
55 |
+
class Completion:
|
56 |
+
def create(
|
57 |
+
prompt: str = 'hello world',
|
58 |
+
messages: list = []) -> SqlchatResponse:
|
59 |
+
|
60 |
+
response = post('https://www.sqlchat.ai/api/chat', headers=headers, stream=True,
|
61 |
+
json = {
|
62 |
+
'messages': messages,
|
63 |
+
'openAIApiConfig':{'key':'','endpoint':''}})
|
64 |
+
|
65 |
+
return SqlchatResponse({
|
66 |
+
'id' : f'cmpl-1337-{int(time())}',
|
67 |
+
'object' : 'text_completion',
|
68 |
+
'created': int(time()),
|
69 |
+
'model' : 'gpt-3.5-turbo',
|
70 |
+
'choices': [{
|
71 |
+
'text' : response.text,
|
72 |
+
'index' : 0,
|
73 |
+
'logprobs' : None,
|
74 |
+
'finish_reason' : 'stop'
|
75 |
+
}],
|
76 |
+
'usage': {
|
77 |
+
'prompt_chars' : len(prompt),
|
78 |
+
'completion_chars' : len(response.text),
|
79 |
+
'total_chars' : len(prompt) + len(response.text)
|
80 |
+
}
|
81 |
+
})
|
82 |
+
|
83 |
+
class StreamCompletion:
|
84 |
+
def create(
|
85 |
+
prompt : str = 'hello world',
|
86 |
+
messages: list = []) -> SqlchatResponse:
|
87 |
+
|
88 |
+
messages.append({
|
89 |
+
'role':'user',
|
90 |
+
'content':prompt
|
91 |
+
})
|
92 |
+
|
93 |
+
response = post('https://www.sqlchat.ai/api/chat', headers=headers, stream=True,
|
94 |
+
json = {
|
95 |
+
'messages': messages,
|
96 |
+
'openAIApiConfig':{'key':'','endpoint':''}})
|
97 |
+
|
98 |
+
for chunk in response.iter_content(chunk_size = 2046):
|
99 |
+
yield SqlchatResponse({
|
100 |
+
'id' : f'cmpl-1337-{int(time())}',
|
101 |
+
'object' : 'text_completion',
|
102 |
+
'created': int(time()),
|
103 |
+
'model' : 'gpt-3.5-turbo',
|
104 |
+
|
105 |
+
'choices': [{
|
106 |
+
'text' : chunk.decode(),
|
107 |
+
'index' : 0,
|
108 |
+
'logprobs' : None,
|
109 |
+
'finish_reason' : 'stop'
|
110 |
+
}],
|
111 |
+
|
112 |
+
'usage': {
|
113 |
+
'prompt_chars' : len(prompt),
|
114 |
+
'completion_chars' : len(chunk.decode()),
|
115 |
+
'total_chars' : len(prompt) + len(chunk.decode())
|
116 |
+
}
|
117 |
+
})
|
testing/sqlchat_test.py
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sqlchat
|
2 |
+
|
3 |
+
for response in sqlchat.StreamCompletion.create(
|
4 |
+
prompt = 'write python code to reverse a string',
|
5 |
+
messages = []):
|
6 |
+
|
7 |
+
print(response.completion.choices[0].text, end='')
|
unfinished/sqlchat/README.md
DELETED
@@ -1,3 +0,0 @@
|
|
1 |
-
https://www.sqlchat.ai/
|
2 |
-
to do:
|
3 |
-
- code refractoring
|
|
|
|
|
|
|
|
unfinished/sqlchat/__init__.py
DELETED
@@ -1,29 +0,0 @@
|
|
1 |
-
import requests
|
2 |
-
|
3 |
-
headers = {
|
4 |
-
'authority': 'www.sqlchat.ai',
|
5 |
-
'accept': '*/*',
|
6 |
-
'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',
|
7 |
-
'content-type': 'text/plain;charset=UTF-8',
|
8 |
-
'origin': 'https://www.sqlchat.ai',
|
9 |
-
'referer': 'https://www.sqlchat.ai/',
|
10 |
-
'sec-fetch-dest': 'empty',
|
11 |
-
'sec-fetch-mode': 'cors',
|
12 |
-
'sec-fetch-site': 'same-origin',
|
13 |
-
'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',
|
14 |
-
}
|
15 |
-
|
16 |
-
data = {
|
17 |
-
'messages':[
|
18 |
-
{'role':'system','content':''},
|
19 |
-
{'role':'user','content':'hello world'},
|
20 |
-
],
|
21 |
-
'openAIApiConfig':{
|
22 |
-
'key':'',
|
23 |
-
'endpoint':''
|
24 |
-
}
|
25 |
-
}
|
26 |
-
|
27 |
-
response = requests.post('https://www.sqlchat.ai/api/chat', headers=headers, json=data, stream=True)
|
28 |
-
for message in response.iter_content(chunk_size=1024):
|
29 |
-
print(message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|