don't url decode ollama
This commit is contained in:
parent
28f5d7beda
commit
cbb30aa3cb
2 changed files with 29 additions and 17 deletions
|
|
@ -117,7 +117,8 @@ class Ollama(LLM):
|
|||
)
|
||||
prompt = llama2_template_messages(messages)
|
||||
|
||||
self.write_log(f"Prompt: {prompt}")
|
||||
self.write_log(f"Prompt:\n{prompt}")
|
||||
completion = ""
|
||||
async with self._client_session.post(
|
||||
f"{self.server_url}/api/generate",
|
||||
json={
|
||||
|
|
@ -127,8 +128,6 @@ class Ollama(LLM):
|
|||
"options": {"temperature": args["temperature"]},
|
||||
},
|
||||
) as resp:
|
||||
# This is streaming application/json instaed of text/event-stream
|
||||
url_decode_buffer = ""
|
||||
async for line in resp.content.iter_chunks():
|
||||
if line[1]:
|
||||
json_chunk = line[0].decode("utf-8")
|
||||
|
|
@ -137,18 +136,12 @@ class Ollama(LLM):
|
|||
if chunk.strip() != "":
|
||||
j = json.loads(chunk)
|
||||
if "response" in j:
|
||||
url_decode_buffer += j["response"]
|
||||
if (
|
||||
"&" in url_decode_buffer
|
||||
and url_decode_buffer.index("&")
|
||||
> len(url_decode_buffer) - 5
|
||||
):
|
||||
continue
|
||||
yield {
|
||||
"role": "assistant",
|
||||
"content": urllib.parse.unquote(url_decode_buffer),
|
||||
"content": j["response"],
|
||||
}
|
||||
url_decode_buffer = ""
|
||||
completion += j["response"]
|
||||
self.write_log(f"Completion:\n{completion}")
|
||||
|
||||
async def complete(
|
||||
self, prompt: str, with_history: List[ChatMessage] = None, **kwargs
|
||||
|
|
|
|||
|
|
@ -24,6 +24,12 @@ openai.api_key = OPENAI_API_KEY
|
|||
FREE_USAGE_STEP_NAME = "Please enter OpenAI API key"
|
||||
|
||||
|
||||
def add_ellipsis(text: str, max_length: int = 200) -> str:
|
||||
if len(text) > max_length:
|
||||
return text[: max_length - 3] + "..."
|
||||
return text
|
||||
|
||||
|
||||
class SimpleChatStep(Step):
|
||||
name: str = "Generating Response..."
|
||||
manage_own_chat_context: bool = True
|
||||
|
|
@ -91,13 +97,26 @@ class SimpleChatStep(Step):
|
|||
|
||||
if "content" in chunk:
|
||||
self.description += chunk["content"]
|
||||
|
||||
# HTML unencode
|
||||
# end_size = len(chunk["content"]) - 6
|
||||
# if "&" in self.description[-end_size:]:
|
||||
# self.description = self.description[:-end_size] + html.unescape(
|
||||
# self.description[-end_size:]
|
||||
# )
|
||||
|
||||
await sdk.update_ui()
|
||||
|
||||
self.name = remove_quotes_and_escapes(
|
||||
await sdk.models.medium.complete(
|
||||
f'"{self.description}"\n\nPlease write a short title summarizing the message quoted above. Use no more than 10 words:',
|
||||
max_tokens=20,
|
||||
)
|
||||
self.name = "Generating title..."
|
||||
await sdk.update_ui()
|
||||
self.name = add_ellipsis(
|
||||
remove_quotes_and_escapes(
|
||||
await sdk.models.medium.complete(
|
||||
f'"{self.description}"\n\nPlease write a short title summarizing the message quoted above. Use no more than 10 words:',
|
||||
max_tokens=20,
|
||||
)
|
||||
),
|
||||
200,
|
||||
)
|
||||
|
||||
self.chat_context.append(
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue