Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0fb2eda

Browse files
author
Szymon Cyranik
committed
feat(llm): add Ollama client implementation
1 parent 9b9a66e commit 0fb2eda

File tree

2 files changed

+107
-1
lines changed

2 files changed

+107
-1
lines changed

mindsql/_utils/constants.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@
3232
OPENAI_VALUE_ERROR = "OpenAI API key is required"
3333
PROMPT_EMPTY_EXCEPTION = "Prompt cannot be empty."
3434
POSTGRESQL_SHOW_CREATE_TABLE_QUERY = """SELECT 'CREATE TABLE "' || table_name || '" (' || array_to_string(array_agg(column_name || ' ' || data_type), ', ') || ');' AS create_statement FROM information_schema.columns WHERE table_name = '{table}' GROUP BY table_name;"""
35-
ANTHROPIC_VALUE_ERROR = "Anthropic API key is required"
35+
ANTHROPIC_VALUE_ERROR = "Anthropic API key is required"
36+
OLLAMA_CONFIG_REQUIRED = "{type} configuration is required."

mindsql/llms/ollama.py

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
from ollama import Client, Options
2+
3+
from .illm import ILlm
4+
from .._utils.constants import PROMPT_EMPTY_EXCEPTION, OLLAMA_CONFIG_REQUIRED
5+
from .._utils import logger
6+
7+
log = logger.init_loggers("Ollama Client")
8+
9+
10+
class Ollama(ILlm):
11+
def __init__(self, model_config: dict, client_config=None, client: Client = None):
12+
"""
13+
Initialize the class with an optional config parameter.
14+
15+
Parameters:
16+
model_config (dict): The model configuration parameter.
17+
config (dict): The configuration parameter.
18+
client (Client): The client parameter.
19+
20+
Returns:
21+
None
22+
"""
23+
self.client = client
24+
self.client_config = client_config
25+
self.model_config = model_config
26+
27+
if self.client is not None:
28+
if self.client_config is not None:
29+
log.warning("Client object provided. Ignoring client_config parameter.")
30+
return
31+
32+
if client_config is None:
33+
raise ValueError(OLLAMA_CONFIG_REQUIRED.format(type="Client"))
34+
35+
if model_config is None:
36+
raise ValueError(OLLAMA_CONFIG_REQUIRED.format(type="Model"))
37+
38+
if 'model' not in model_config:
39+
raise ValueError(OLLAMA_CONFIG_REQUIRED.format(type="Model name"))
40+
41+
self.client = Client(**client_config)
42+
43+
def system_message(self, message: str) -> any:
44+
"""
45+
Create a system message.
46+
47+
Parameters:
48+
message (str): The message parameter.
49+
50+
Returns:
51+
any
52+
"""
53+
return {"role": "system", "content": message}
54+
55+
def user_message(self, message: str) -> any:
56+
"""
57+
Create a user message.
58+
59+
Parameters:
60+
message (str): The message parameter.
61+
62+
Returns:
63+
any
64+
"""
65+
return {"role": "user", "content": message}
66+
67+
def assistant_message(self, message: str) -> any:
68+
"""
69+
Create an assistant message.
70+
71+
Parameters:
72+
message (str): The message parameter.
73+
74+
Returns:
75+
any
76+
"""
77+
return {"role": "assistant", "content": message}
78+
79+
def invoke(self, prompt, **kwargs) -> str:
80+
"""
81+
Submit a prompt to the model for generating a response.
82+
83+
Parameters:
84+
prompt (str): The prompt parameter.
85+
**kwargs: Additional keyword arguments (optional).
86+
- temperature (float): The temperature parameter for controlling randomness in generation.
87+
88+
Returns:
89+
str
90+
"""
91+
if not prompt:
92+
raise ValueError(PROMPT_EMPTY_EXCEPTION)
93+
94+
model = self.model_config.get('model')
95+
temperature = kwargs.get('temperature', 0.1)
96+
97+
response = self.client.chat(
98+
model=model,
99+
messages=[self.user_message(prompt)],
100+
options=Options(
101+
temperature=temperature
102+
)
103+
)
104+
105+
return response['message']['content']

0 commit comments

Comments
 (0)