Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
artitw committed Oct 7, 2024
1 parent 0570d3e commit b838be8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="text2text",
version="1.6.8",
version="1.6.9",
author="artitw",
author_email="artitw@gmail.com",
description="Text2Text: Crosslingual NLP/G toolkit",
Expand Down
24 changes: 9 additions & 15 deletions text2text/assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,28 +69,22 @@ def load_model(self):
self.ollama_serve_proc = subprocess.Popen(["ollama", "serve"])
time.sleep(1)

result = subprocess.check_output(
["ollama", "-v"],
stderr=subprocess.STDOUT
).decode("utf-8")
if not result.startswith("ollama version"):
raise Exception(result)
if not ollama_version():
raise Exception("Cannot serve ollama")

result = ollama.pull(self.model_name)
if result["status"] != "success":
raise Exception(f"Did not pull {self.model_name}.")

ollama_run_proc = subprocess.Popen(["ollama", "run", self.model_name])
if result["status"] == "success":
ollama_run_proc = subprocess.Popen(["ollama", "run", self.model_name])
else:
raise Exception(f"Did not pull {self.model_name}. Try restarting.")

def chat_completion(self, messages=[{"role": "user", "content": "hello"}], stream=False, schema=None, **kwargs):
try:
result = ollama.ps()
if not result or not result.get("models"):
result = ollama.pull(self.model_name)
if result["status"] == "success":
ollama_run_proc = subprocess.Popen(["ollama", "run", self.model_name])
return self.chat_completion(messages=messages, stream=stream, **kwargs)
raise Exception(f"Did not pull {self.model_name}. Try restarting.")
warnings.warn("No model loaded. Retrying...")
self.load_model()
return self.chat_completion(messages=messages, stream=stream, **kwargs)
except Exception as e:
warnings.warn(str(e))
warnings.warn("Retrying...")
Expand Down

0 comments on commit b838be8

Please sign in to comment.