forked from irmen/Tale
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from neph1/update-v0.21.1
Update v0.21.1
- Loading branch information
Showing
15 changed files
with
284 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"events": {}, | ||
"looks": {}, | ||
"tells": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,5 +9,6 @@ pillow | |
packaging==20.3 | ||
pillow>=8.3.2 | ||
responses==0.13.3 | ||
aioresponses==0.7.6 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
|
||
from abc import ABC, abstractmethod | ||
import asyncio | ||
import json | ||
import time | ||
|
||
import aiohttp | ||
import requests | ||
|
||
from tale.errors import LlmResponseException | ||
|
||
|
||
class AbstractIoAdapter(ABC): | ||
|
||
def __init__(self, url: str, stream_endpoint: str, user_start_prompt: str, user_end_prompt: str): | ||
self.url = url | ||
self.stream_endpoint = stream_endpoint | ||
self.user_start_prompt = user_start_prompt | ||
self.user_end_prompt = user_end_prompt | ||
|
||
@abstractmethod | ||
def stream_request(self, request_body: dict, io = None, wait: bool = False) -> str: | ||
pass | ||
|
||
@abstractmethod | ||
async def _do_stream_request(self, url: str, request_body: dict,) -> bool: | ||
pass | ||
|
||
@abstractmethod | ||
def _parse_result(self, result: str) -> str: | ||
pass | ||
|
||
@abstractmethod | ||
def _set_prompt(self, request_body: dict, prompt: str, context: str = '') -> dict: | ||
pass | ||
|
||
class KoboldCppAdapter(AbstractIoAdapter): | ||
|
||
def __init__(self, url: str, stream_endpoint: str, data_endpoint: str, user_start_prompt: str, user_end_prompt: str): | ||
super().__init__(url, stream_endpoint, user_start_prompt, user_end_prompt) | ||
self.data_endpoint = data_endpoint | ||
|
||
def stream_request(self, request_body: dict, io = None, wait: bool = False) -> str: | ||
result = asyncio.run(self._do_stream_request(self.url + self.stream_endpoint, request_body)) | ||
|
||
try: | ||
if result: | ||
return self._do_process_result(self.url + self.data_endpoint, io, wait) | ||
except LlmResponseException as exc: | ||
print("Error parsing response from backend - ", exc) | ||
return '' | ||
|
||
async def _do_stream_request(self, url: str, request_body: dict,) -> bool: | ||
""" Send request to stream endpoint async to not block the main thread""" | ||
async with aiohttp.ClientSession() as session: | ||
async with session.post(url, data=json.dumps(request_body)) as response: | ||
if response.status == 200: | ||
return True | ||
else: | ||
print("Error occurred:", response.status) | ||
|
||
def _do_process_result(self, url, io = None, wait: bool = False) -> str: | ||
""" Process the result from the stream endpoint """ | ||
tries = 0 | ||
old_text = '' | ||
while tries < 4: | ||
time.sleep(0.25) | ||
data = requests.post(url) | ||
|
||
text = json.loads(data.text)['results'][0]['text'] | ||
|
||
if len(text) == len(old_text): | ||
tries += 1 | ||
continue | ||
if not wait: | ||
new_text = text[len(old_text):] | ||
io.output_no_newline(new_text, new_paragraph=False) | ||
old_text = text | ||
return old_text | ||
|
||
def _parse_result(self, result: str) -> str: | ||
""" Parse the result from the stream endpoint """ | ||
return json.loads(result)['results'][0]['text'] | ||
|
||
def _set_prompt(self, request_body: dict, prompt: str, context: str = '') -> dict: | ||
if self.user_start_prompt: | ||
prompt = prompt.replace('[USER_START]', self.user_start_prompt) | ||
if self.user_end_prompt: | ||
prompt = prompt + self.user_end_prompt | ||
prompt.replace('<context>{context}</context>', '') | ||
request_body['prompt'] = prompt | ||
request_body['memory'] = context | ||
return request_body | ||
|
||
class LlamaCppAdapter(AbstractIoAdapter): | ||
|
||
def stream_request(self, request_body: dict, io = None, wait: bool = False) -> str: | ||
return asyncio.run(self._do_stream_request(self.url + self.stream_endpoint, request_body, io = io)) | ||
|
||
async def _do_stream_request(self, url: str, request_body: dict, io = None) -> str: | ||
""" Send request to stream endpoint async to not block the main thread""" | ||
request_body['stream'] = True | ||
text = '' | ||
async with aiohttp.ClientSession() as session: | ||
async with session.post(url, data=json.dumps(request_body)) as response: | ||
if response.status != 200: | ||
print("Error occurred:", response.status) | ||
return False | ||
async for chunk in response.content.iter_any(): | ||
decoded = chunk.decode('utf-8') | ||
lines = decoded.split('\n') | ||
for line in lines: | ||
# Ignore empty lines | ||
if not line.strip(): | ||
continue | ||
key, value = line.split(':', 1) | ||
key = key.strip() | ||
value = value.strip() | ||
if key == 'data': | ||
data = json.loads(value) | ||
choice = data['choices'][0]['delta'] | ||
content = choice.get('content', None) | ||
|
||
if content: | ||
io.output_no_newline(content, new_paragraph=False) | ||
text += content | ||
#while len(lines) == 0: | ||
# await asyncio.sleep(0.05) | ||
|
||
return text | ||
|
||
def _parse_result(self, result: str) -> str: | ||
""" Parse the result from the stream endpoint """ | ||
try: | ||
return json.loads(result)['choices'][0]['message']['content'] | ||
except: | ||
raise LlmResponseException("Error parsing result from backend") | ||
|
||
def _set_prompt(self, request_body: dict, prompt: str, context: str = '') -> dict: | ||
if self.user_start_prompt: | ||
prompt = prompt.replace('[USER_START]', self.user_start_prompt) | ||
if self.user_end_prompt: | ||
prompt = prompt + self.user_end_prompt | ||
if context: | ||
prompt = prompt.format(context=context) | ||
request_body['messages'][1]['content'] = prompt | ||
return request_body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.