Adapters
GitlabClientAdapter
¶
Bases: GitServerProtocol
Adapter class for interacting with a GitLab server using its API.
This class provides methods for authenticated communication with a GitLab server. It supports caching of API responses when specified, and it facilitates requests for retrieving and sending data to GitLab projects via its REST API.
This class implements the GitServerProtocol interface, which serves as a contract
for common operations performed by git server clients. It is designed to handle
multiple types of authentication and provides error handling for common HTTP request
issues.
Attributes:
| Name | Type | Description |
|---|---|---|
base_url |
str
|
The base URL for the GitLab API. |
private_token |
SensitiveConfigValue
|
A personal access token for GitLab API authentication. |
client |
Client
|
The HTTP session with GitLab. |
__init__(base_url, private_token)
¶
Initialise the GitLab API client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_url
|
str
|
Base URL for GitLab API |
required |
private_token
|
SensitiveConfigValue
|
Personal access token for authentication |
required |
check_health()
¶
Check if GitLab is accessible with current credentials.
get_from_server(project_id, needs_auth=True, api_path='', query_string='', expect_json=True, expect_pagination=False)
¶
Fetches resource data from Gitlab API via an HTTP GET request.
This method communicates with a specified Git server API using the
provided project details, query string, and additional customisable
parameters. It includes support for authentication and allows
retrieval of data in JSON or raw content format based on the
expect_json flag. Error handling is implemented for various
HTTP and connectivity-related issues.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
project_id
|
int | str
|
The ID of the project to retrieve data for. |
required |
needs_auth
|
bool
|
Indicates if authentication is required for the request. Defaults to True. |
True
|
api_path
|
str
|
The relative path of the API endpoint. Defaults to an empty string. |
''
|
query_string
|
str
|
The URL query string to append to the endpoint. Defaults to an empty string. |
''
|
expect_json
|
bool
|
Indicates if the response is expected to be in JSON format. Defaults to True. |
True
|
expect_pagination
|
bool
|
Indicates if the response is paginated.
Implies |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The parsed JSON response if |
Any
|
the raw content of the response. |
Raises:
| Type | Description |
|---|---|
RAGitServerError
|
If an error occurs while connecting to the server, due to timeouts, invalid URLs, or unsuccessful HTTP responses. |
get_page_size()
¶
Returns the page size for pagination.
post_to_server(project_id, api_path='', data=None)
¶
Sends a POST request to the GitLab API with the specified arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
project_id
|
int | str
|
The ID or name of the project in GitLab. |
required |
api_path
|
str
|
The relative API path for the POST operation. |
''
|
data
|
Any
|
The payload to be sent with the POST request. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The response from the GitLab server, parsed as JSON. |
Raises:
| Type | Description |
|---|---|
RAGitServerError
|
Raised when there is a timeout, connection error, invalid URL, or an HTTP status error during communication with the GitLab API. |
LLMProviderProtocol
¶
Bases: Protocol
Protocol for local AI providers.
This protocol defines the interface that all local AI providers must implement. It ensures type safety and consistent behaviour across different AI provider implementations.
Methods:
| Name | Description |
|---|---|
build_chat_context |
Build the chat context for a conversational AI |
instruct |
Send an instruction to the AI model and receive a response |
check_health |
Check the health of the AI provider |
build_chat_context(system_prompt, user_prompt, diffs)
¶
Builds the chat context for a conversational AI.
This method is responsible for constructing a chat context based on the given system prompt, user inputs, and a list of differences or changes. It serves as an abstract method that needs to be implemented by subclasses to provide specific behaviour for constructing the chat context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system_prompt
|
str
|
A string representing the system-level prompt that provides context or guidelines for the chat session. |
required |
user_prompt
|
list[str]
|
A list of strings containing user inputs or messages that are part of the conversation. |
required |
diffs
|
Diffs
|
A list of dictionaries representing differences, changes, or additional context required for the chat context. |
required |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If the method is not implemented by a subclass. |
Returns:
| Name | Type | Description |
|---|---|---|
ChatContext |
ChatContext
|
The constructed chat context based on |
ChatContext
|
the inputs. |
check_health()
¶
Check the health of the AI provider.
instruct(context)
¶
Provides an interface for generating predictions based on a given prompt.
The instruct function should be implemented by subclasses to provide the
specific logic for handling the prompt and generating predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
ChatContext
|
The input provided for generating predictions. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Predictions |
Predictions
|
The response generated based on the given prompt. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If called on the base class without an implementation in a subclass. |
LMStudioAdapter
¶
Bases: LLMProviderProtocol
Client for interacting with LMStudio local AI service.
This client provides an interface to send instructions to a local LMStudio AI model and retrieve responses. It implements the LocalProvider protocol for local AI providers.
Examples:
>>> from reviewassist.adapters import LMStudioAdapter
>>> client = LMStudioAdapter()
>>> prompt = Chat(messages=[{'role': 'user', 'content': 'Hello!'}])
>>> response = client.instruct(prompt)
Attributes:
| Name | Type | Description |
|---|---|---|
model |
The LMStudio model instance |
|
model_name |
The name of the selected model |
|
response |
PredictionResult | None
|
Storage for the last prediction result |
__init__(model_name=DEFAULT_MODEL)
¶
Initialise the LMStudio client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name
|
str
|
The name of the model to use (default: DEFAULT_MODEL) |
DEFAULT_MODEL
|
build_chat_context(system_prompt, user_prompt, diffs)
¶
Create a chat context for the AI model.
check_health()
¶
Check if LMStudio is running and accessible.
instruct(context)
¶
Send an instruction to the AI model and return the response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
ChatContext
|
The instruction prompt to send to the model as a Chat object |
required |
Returns:
| Name | Type | Description |
|---|---|---|
PredictionResult |
PredictionResult
|
The model's response wrapped in PredictionResult |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the model fails to generate a response |
ValueError
|
If the prompt is malformed or invalid |