From c6012a5fed1023bb95c18fd8e4b02c78e0055fdf Mon Sep 17 00:00:00 2001 From: Cyber MacGeddon Date: Thu, 25 Sep 2025 21:20:27 +0100 Subject: [PATCH] More LLMs --- .../model/text_completion/cohere/llm.py | 7 ++++-- .../model/text_completion/llamafile/llm.py | 23 +++++++++++-------- .../model/text_completion/lmstudio/llm.py | 23 +++++++++++-------- .../model/text_completion/mistral/llm.py | 7 ++++-- .../model/text_completion/ollama/llm.py | 18 +++++++++++++-- .../model/text_completion/openai/llm.py | 7 ++++-- .../model/text_completion/tgi/llm.py | 7 ++++-- 7 files changed, 62 insertions(+), 30 deletions(-) diff --git a/trustgraph-flow/trustgraph/model/text_completion/cohere/llm.py b/trustgraph-flow/trustgraph/model/text_completion/cohere/llm.py index a5b1deda..9aebaa73 100755 --- a/trustgraph-flow/trustgraph/model/text_completion/cohere/llm.py +++ b/trustgraph-flow/trustgraph/model/text_completion/cohere/llm.py @@ -45,12 +45,15 @@ class Processor(LlmService): logger.info("Cohere LLM service initialized") - async def generate_content(self, system, prompt, model=None): + async def generate_content(self, system, prompt, model=None, temperature=None): # Use provided model or fall back to default model_name = model or self.default_model + # Use provided temperature or fall back to default + effective_temperature = temperature if temperature is not None else self.temperature logger.debug(f"Using model: {model_name}") + logger.debug(f"Using temperature: {effective_temperature}") try: @@ -58,7 +61,7 @@ class Processor(LlmService): model=model_name, message=prompt, preamble = system, - temperature=self.temperature, + temperature=effective_temperature, chat_history=[], prompt_truncation='auto', connectors=[] diff --git a/trustgraph-flow/trustgraph/model/text_completion/llamafile/llm.py b/trustgraph-flow/trustgraph/model/text_completion/llamafile/llm.py index 1571e3e7..2b343583 100755 --- a/trustgraph-flow/trustgraph/model/text_completion/llamafile/llm.py +++ b/trustgraph-flow/trustgraph/model/text_completion/llamafile/llm.py @@ -50,12 +50,15 @@ class Processor(LlmService): logger.info("Llamafile LLM service initialized") - async def generate_content(self, system, prompt, model=None): + async def generate_content(self, system, prompt, model=None, temperature=None): # Use provided model or fall back to default model_name = model or self.default_model + # Use provided temperature or fall back to default + effective_temperature = temperature if temperature is not None else self.temperature logger.debug(f"Using model: {model_name}") + logger.debug(f"Using temperature: {effective_temperature}") prompt = system + "\n\n" + prompt @@ -65,15 +68,15 @@ class Processor(LlmService): model=model_name, 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" - #} + ], + temperature=effective_temperature, + max_tokens=self.max_output, + top_p=1, + frequency_penalty=0, + presence_penalty=0, + response_format={ + "type": "text" + } ) inputtokens = resp.usage.prompt_tokens diff --git a/trustgraph-flow/trustgraph/model/text_completion/lmstudio/llm.py b/trustgraph-flow/trustgraph/model/text_completion/lmstudio/llm.py index 0ed47517..a5464368 100755 --- a/trustgraph-flow/trustgraph/model/text_completion/lmstudio/llm.py +++ b/trustgraph-flow/trustgraph/model/text_completion/lmstudio/llm.py @@ -50,12 +50,15 @@ class Processor(LlmService): logger.info("LMStudio LLM service initialized") - async def generate_content(self, system, prompt, model=None): + async def generate_content(self, system, prompt, model=None, temperature=None): # Use provided model or fall back to default model_name = model or self.default_model + # Use provided temperature or fall back to default + effective_temperature = temperature if temperature is not None else self.temperature logger.debug(f"Using model: {model_name}") + logger.debug(f"Using temperature: {effective_temperature}") prompt = system + "\n\n" + prompt @@ -67,15 +70,15 @@ class Processor(LlmService): model=model_name, 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" - #} + ], + temperature=effective_temperature, + max_tokens=self.max_output, + top_p=1, + frequency_penalty=0, + presence_penalty=0, + response_format={ + "type": "text" + } ) logger.debug(f"Full response: {resp}") diff --git a/trustgraph-flow/trustgraph/model/text_completion/mistral/llm.py b/trustgraph-flow/trustgraph/model/text_completion/mistral/llm.py index c4ce26d5..bcd00a0c 100755 --- a/trustgraph-flow/trustgraph/model/text_completion/mistral/llm.py +++ b/trustgraph-flow/trustgraph/model/text_completion/mistral/llm.py @@ -48,12 +48,15 @@ class Processor(LlmService): logger.info("Mistral LLM service initialized") - async def generate_content(self, system, prompt, model=None): + async def generate_content(self, system, prompt, model=None, temperature=None): # Use provided model or fall back to default model_name = model or self.default_model + # Use provided temperature or fall back to default + effective_temperature = temperature if temperature is not None else self.temperature logger.debug(f"Using model: {model_name}") + logger.debug(f"Using temperature: {effective_temperature}") prompt = system + "\n\n" + prompt @@ -72,7 +75,7 @@ class Processor(LlmService): ] } ], - temperature=self.temperature, + temperature=effective_temperature, max_tokens=self.max_output, top_p=1, frequency_penalty=0, diff --git a/trustgraph-flow/trustgraph/model/text_completion/ollama/llm.py b/trustgraph-flow/trustgraph/model/text_completion/ollama/llm.py index fc19ace3..db9586ea 100755 --- a/trustgraph-flow/trustgraph/model/text_completion/ollama/llm.py +++ b/trustgraph-flow/trustgraph/model/text_completion/ollama/llm.py @@ -17,6 +17,7 @@ from .... base import LlmService, LlmResult default_ident = "text-completion" default_model = 'gemma2:9b' +default_temperature = 0.0 default_ollama = os.getenv("OLLAMA_HOST", 'http://localhost:11434') class Processor(LlmService): @@ -24,30 +25,36 @@ class Processor(LlmService): def __init__(self, **params): model = params.get("model", default_model) + temperature = params.get("temperature", default_temperature) ollama = params.get("ollama", default_ollama) super(Processor, self).__init__( **params | { "model": model, + "temperature": temperature, "ollama": ollama, } ) self.default_model = model + self.temperature = temperature self.llm = Client(host=ollama) - async def generate_content(self, system, prompt, model=None): + async def generate_content(self, system, prompt, model=None, temperature=None): # Use provided model or fall back to default model_name = model or self.default_model + # Use provided temperature or fall back to default + effective_temperature = temperature if temperature is not None else self.temperature logger.debug(f"Using model: {model_name}") + logger.debug(f"Using temperature: {effective_temperature}") prompt = system + "\n\n" + prompt try: - response = self.llm.generate(model_name, prompt) + response = self.llm.generate(model_name, prompt, options={'temperature': effective_temperature}) response_text = response['response'] logger.debug("Sending response...") @@ -89,6 +96,13 @@ class Processor(LlmService): help=f'ollama (default: {default_ollama})' ) + parser.add_argument( + '-t', '--temperature', + type=float, + default=default_temperature, + help=f'LLM temperature parameter (default: {default_temperature})' + ) + def run(): Processor.launch(default_ident, __doc__) diff --git a/trustgraph-flow/trustgraph/model/text_completion/openai/llm.py b/trustgraph-flow/trustgraph/model/text_completion/openai/llm.py index 79e0c86f..74ed2353 100755 --- a/trustgraph-flow/trustgraph/model/text_completion/openai/llm.py +++ b/trustgraph-flow/trustgraph/model/text_completion/openai/llm.py @@ -58,12 +58,15 @@ class Processor(LlmService): logger.info("OpenAI LLM service initialized") - async def generate_content(self, system, prompt, model=None): + async def generate_content(self, system, prompt, model=None, temperature=None): # Use provided model or fall back to default model_name = model or self.default_model + # Use provided temperature or fall back to default + effective_temperature = temperature if temperature is not None else self.temperature logger.debug(f"Using model: {model_name}") + logger.debug(f"Using temperature: {effective_temperature}") prompt = system + "\n\n" + prompt @@ -82,7 +85,7 @@ class Processor(LlmService): ] } ], - temperature=self.temperature, + temperature=effective_temperature, max_tokens=self.max_output, top_p=1, frequency_penalty=0, diff --git a/trustgraph-flow/trustgraph/model/text_completion/tgi/llm.py b/trustgraph-flow/trustgraph/model/text_completion/tgi/llm.py index c8622c85..9bfc58be 100755 --- a/trustgraph-flow/trustgraph/model/text_completion/tgi/llm.py +++ b/trustgraph-flow/trustgraph/model/text_completion/tgi/llm.py @@ -51,12 +51,15 @@ class Processor(LlmService): logger.info(f"Using TGI service at {base_url}") logger.info("TGI LLM service initialized") - async def generate_content(self, system, prompt, model=None): + async def generate_content(self, system, prompt, model=None, temperature=None): # Use provided model or fall back to default model_name = model or self.default_model + # Use provided temperature or fall back to default + effective_temperature = temperature if temperature is not None else self.temperature logger.debug(f"Using model: {model_name}") + logger.debug(f"Using temperature: {effective_temperature}") headers = { "Content-Type": "application/json", @@ -75,7 +78,7 @@ class Processor(LlmService): } ], "max_tokens": self.max_output, - "temperature": self.temperature, + "temperature": effective_temperature, } try: