KAG/kag/common/llm/ollama_client.py

104 lines
3.2 KiB
Python

# -*- coding: utf-8 -*-
# Copyright 2023 OpenSPG Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
# in compliance with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
# or implied.
import json
import logging
from ollama import Client
from kag.interface import LLMClient
from tenacity import retry, stop_after_attempt
# logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
@LLMClient.register("Ollama")
@LLMClient.register("ollama")
class OllamaClient(LLMClient):
"""
A client class for interacting with the Ollama API.
This class provides methods to make synchronous requests to the Ollama API, handle model calls, and parse responses.
"""
def __init__(self, model: str, base_url: str):
"""
Initializes the OllamaClient instance.
Args:
model (str): The model to use for requests.
base_url (str): The base URL for the Ollama API.
"""
self.model = model
self.base_url = base_url
self.param = {}
self.client = Client(host=self.base_url)
self.check()
def sync_request(self, prompt, image=None):
"""
Makes a synchronous request to the Ollama API with the given prompt.
Args:
prompt: The prompt to send to the Ollama API.
image: Optional image data to include in the request.
Returns:
str: The content of the response from the Ollama API.
"""
response = self.client.generate(model=self.model, prompt=prompt, stream=False)
content = response["response"]
content = content.replace("”", "").replace("“", "")
content = content.replace("·", "")
return content
def __call__(self, prompt, image=None):
"""
Executes a model request when the object is called and returns the result.
Parameters:
prompt (str): The prompt provided to the model.
Returns:
str: The response content generated by the model.
"""
return self.sync_request(prompt, image)
@retry(stop=stop_after_attempt(3))
def call_with_json_parse(self, prompt):
"""
Calls the model and attempts to parse the response into JSON format.
Parameters:
prompt (str): The prompt provided to the model.
Returns:
Union[dict, str]: If the response is valid JSON, returns the parsed dictionary; otherwise, returns the original response.
"""
rsp = self.sync_request(prompt)
_end = rsp.rfind("```")
_start = rsp.find("```json")
if _end != -1 and _start != -1:
json_str = rsp[_start + len("```json") : _end].strip()
else:
json_str = rsp
try:
json_result = json.loads(json_str)
except:
return rsp
return json_result