mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-17 09:11:03 +02:00
Added basic Llamafile integration
This commit is contained in:
parent
6af86fa09f
commit
c7cd97b6d3
6 changed files with 214 additions and 2 deletions
2
Makefile
2
Makefile
|
|
@ -1,6 +1,6 @@
|
||||||
|
|
||||||
# VERSION=$(shell git describe | sed 's/^v//')
|
# VERSION=$(shell git describe | sed 's/^v//')
|
||||||
VERSION=0.9.4
|
VERSION=0.9.5
|
||||||
|
|
||||||
DOCKER=podman
|
DOCKER=podman
|
||||||
|
|
||||||
|
|
|
||||||
6
scripts/text-completion-llamafile
Executable file
6
scripts/text-completion-llamafile
Executable file
|
|
@ -0,0 +1,6 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
from trustgraph.model.text_completion.llamafile import run
|
||||||
|
|
||||||
|
run()
|
||||||
|
|
||||||
3
setup.py
3
setup.py
|
|
@ -4,7 +4,7 @@ import os
|
||||||
with open("README.md", "r") as fh:
|
with open("README.md", "r") as fh:
|
||||||
long_description = fh.read()
|
long_description = fh.read()
|
||||||
|
|
||||||
version = "0.9.4"
|
version = "0.9.5"
|
||||||
|
|
||||||
setuptools.setup(
|
setuptools.setup(
|
||||||
name="trustgraph",
|
name="trustgraph",
|
||||||
|
|
@ -94,6 +94,7 @@ setuptools.setup(
|
||||||
"scripts/text-completion-bedrock",
|
"scripts/text-completion-bedrock",
|
||||||
"scripts/text-completion-claude",
|
"scripts/text-completion-claude",
|
||||||
"scripts/text-completion-cohere",
|
"scripts/text-completion-cohere",
|
||||||
|
"scripts/text-completion-llamafile",
|
||||||
"scripts/text-completion-ollama",
|
"scripts/text-completion-ollama",
|
||||||
"scripts/text-completion-openai",
|
"scripts/text-completion-openai",
|
||||||
"scripts/text-completion-vertexai",
|
"scripts/text-completion-vertexai",
|
||||||
|
|
|
||||||
3
trustgraph/model/text_completion/llamafile/__init__.py
Normal file
3
trustgraph/model/text_completion/llamafile/__init__.py
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
|
||||||
|
from . llm import *
|
||||||
|
|
||||||
7
trustgraph/model/text_completion/llamafile/__main__.py
Executable file
7
trustgraph/model/text_completion/llamafile/__main__.py
Executable file
|
|
@ -0,0 +1,7 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
from . llm import run
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
run()
|
||||||
|
|
||||||
195
trustgraph/model/text_completion/llamafile/llm.py
Executable file
195
trustgraph/model/text_completion/llamafile/llm.py
Executable file
|
|
@ -0,0 +1,195 @@
|
||||||
|
|
||||||
|
"""
|
||||||
|
Simple LLM service, performs text prompt completion using OpenAI.
|
||||||
|
Input is prompt, output is response.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from openai import OpenAI
|
||||||
|
from prometheus_client import Histogram
|
||||||
|
|
||||||
|
from .... schema import TextCompletionRequest, TextCompletionResponse, Error
|
||||||
|
from .... schema import text_completion_request_queue
|
||||||
|
from .... schema import text_completion_response_queue
|
||||||
|
from .... log_level import LogLevel
|
||||||
|
from .... base import ConsumerProducer
|
||||||
|
from .... exceptions import TooManyRequests
|
||||||
|
|
||||||
|
module = ".".join(__name__.split(".")[1:-1])
|
||||||
|
|
||||||
|
default_input_queue = text_completion_request_queue
|
||||||
|
default_output_queue = text_completion_response_queue
|
||||||
|
default_subscriber = module
|
||||||
|
default_model = 'LLaMA_CPP'
|
||||||
|
default_llamafile = 'http://localhost:8080/v1'
|
||||||
|
default_temperature = 0.0
|
||||||
|
default_max_output = 4096
|
||||||
|
|
||||||
|
class Processor(ConsumerProducer):
|
||||||
|
|
||||||
|
def __init__(self, **params):
|
||||||
|
|
||||||
|
input_queue = params.get("input_queue", default_input_queue)
|
||||||
|
output_queue = params.get("output_queue", default_output_queue)
|
||||||
|
subscriber = params.get("subscriber", default_subscriber)
|
||||||
|
model = params.get("model", default_model)
|
||||||
|
llamafile = params.get("llamafile", default_llamafile)
|
||||||
|
temperature = params.get("temperature", default_temperature)
|
||||||
|
max_output = params.get("max_output", default_max_output)
|
||||||
|
|
||||||
|
super(Processor, self).__init__(
|
||||||
|
**params | {
|
||||||
|
"input_queue": input_queue,
|
||||||
|
"output_queue": output_queue,
|
||||||
|
"subscriber": subscriber,
|
||||||
|
"input_schema": TextCompletionRequest,
|
||||||
|
"output_schema": TextCompletionResponse,
|
||||||
|
"model": model,
|
||||||
|
"temperature": temperature,
|
||||||
|
"max_output": max_output,
|
||||||
|
"llamafile" : llamafile,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
if not hasattr(__class__, "text_completion_metric"):
|
||||||
|
__class__.text_completion_metric = Histogram(
|
||||||
|
'text_completion_duration',
|
||||||
|
'Text completion duration (seconds)',
|
||||||
|
buckets=[
|
||||||
|
0.25, 0.5, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0,
|
||||||
|
8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0,
|
||||||
|
17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0,
|
||||||
|
30.0, 35.0, 40.0, 45.0, 50.0, 60.0, 80.0, 100.0,
|
||||||
|
120.0
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.model = model
|
||||||
|
self.llamafile=llamafile
|
||||||
|
self.temperature = temperature
|
||||||
|
self.max_output = max_output
|
||||||
|
self.openai = OpenAI(
|
||||||
|
base_url=self.llamafile,
|
||||||
|
api_key = "sk-no-key-required",
|
||||||
|
)
|
||||||
|
|
||||||
|
print("Initialised", flush=True)
|
||||||
|
|
||||||
|
def handle(self, msg):
|
||||||
|
|
||||||
|
v = msg.value()
|
||||||
|
|
||||||
|
# Sender-produced ID
|
||||||
|
|
||||||
|
id = msg.properties()["id"]
|
||||||
|
|
||||||
|
print(f"Handling prompt {id}...", flush=True)
|
||||||
|
|
||||||
|
prompt = v.prompt
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
# FIXME: Rate limits
|
||||||
|
|
||||||
|
with __class__.text_completion_metric.time():
|
||||||
|
|
||||||
|
resp = self.openai.chat.completions.create(
|
||||||
|
model=self.model,
|
||||||
|
messages=[
|
||||||
|
{"role": "user", "content": prompt}
|
||||||
|
]
|
||||||
|
#temperature=self.temperature,
|
||||||
|
#max_tokens=self.max_output,
|
||||||
|
#top_p=1,
|
||||||
|
#frequency_penalty=0,
|
||||||
|
#presence_penalty=0,
|
||||||
|
#response_format={
|
||||||
|
# "type": "text"
|
||||||
|
#}
|
||||||
|
)
|
||||||
|
|
||||||
|
print(resp.choices[0].message.content, flush=True)
|
||||||
|
|
||||||
|
print("Send response...", flush=True)
|
||||||
|
r = TextCompletionResponse(
|
||||||
|
response=resp.choices[0].message.content,
|
||||||
|
error=None,
|
||||||
|
)
|
||||||
|
self.send(r, properties={"id": id})
|
||||||
|
|
||||||
|
print("Done.", flush=True)
|
||||||
|
|
||||||
|
# FIXME: Wrong exception, don't know what this LLM throws
|
||||||
|
# for a rate limit
|
||||||
|
except TooManyRequests:
|
||||||
|
|
||||||
|
print("Send rate limit response...", flush=True)
|
||||||
|
|
||||||
|
r = TextCompletionResponse(
|
||||||
|
error=Error(
|
||||||
|
type = "rate-limit",
|
||||||
|
message = str(e),
|
||||||
|
),
|
||||||
|
response=None,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.producer.send(r, properties={"id": id})
|
||||||
|
|
||||||
|
self.consumer.acknowledge(msg)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
|
||||||
|
print(f"Exception: {e}")
|
||||||
|
|
||||||
|
print("Send error response...", flush=True)
|
||||||
|
|
||||||
|
r = TextCompletionResponse(
|
||||||
|
error=Error(
|
||||||
|
type = "llm-error",
|
||||||
|
message = str(e),
|
||||||
|
),
|
||||||
|
response=None,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.producer.send(r, properties={"id": id})
|
||||||
|
|
||||||
|
self.consumer.acknowledge(msg)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def add_args(parser):
|
||||||
|
|
||||||
|
ConsumerProducer.add_args(
|
||||||
|
parser, default_input_queue, default_subscriber,
|
||||||
|
default_output_queue,
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
'-m', '--model',
|
||||||
|
default=default_model,
|
||||||
|
help=f'LLM model (default: LLaMA_CPP)'
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
'-r', '--llamafile',
|
||||||
|
default=default_llamafile,
|
||||||
|
help=f'ollama (default: {default_llamafile})'
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
'-t', '--temperature',
|
||||||
|
type=float,
|
||||||
|
default=default_temperature,
|
||||||
|
help=f'LLM temperature parameter (default: {default_temperature})'
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
'-x', '--max-output',
|
||||||
|
type=int,
|
||||||
|
default=default_max_output,
|
||||||
|
help=f'LLM max output tokens (default: {default_max_output})'
|
||||||
|
)
|
||||||
|
|
||||||
|
def run():
|
||||||
|
|
||||||
|
Processor.start(module, __doc__)
|
||||||
|
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue