Skip to content

feat(ai): llm provider #539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from

Conversation

kallebysantos
Copy link
Contributor

@kallebysantos kallebysantos commented May 10, 2025

What kind of change does this PR introduce?

feature, refactor

What is the current behaviour?

Current the Session only supports self-hosted Ollama or some OpenAI like provider - with no way to specify the API key.

What is the new behaviour?

This PR applies some refactors in the ai module to support an unified LLM provider API, this way it can be easily extended to new providers as well exporting a more standardised output format.

Improved typescript support

The ai module was huge refactored to provide better ts hints that dynamically changes based on the selected type:

examples

using type gte-small:
image

using type ollama:
image

using type openaicompatible:
image

Automatically infer AsyncGenerator type when stream: true

All providers, ollama & openai

image

Improved error handling support

In order to ensure error checking, the ai module was been refactored to follow Result pattern - Go like. It means that while Session.run() the returned value will be a tuple array of [success, error], this result is compatible with TS pattern matching, so it provides a completely LSP feedback.

examples

Non stream

Result type def

image

Checking error automatically validates the success part

image
image

Stream

When stream: true the first result will handle errors that may occur before create the AsyncGenerator.
Then the incoming message will be a result as well, this way users can apply error handling while streaming.

Result type def
image

Streaming type def
image

Common response and Usage metrics

Since all LLM providers must implement a common interface they now also shared a unified response object.

response definitions

Success part

// This object represents a success LLM output, where inner is the raw value returned by the provider
export interface ILLMProviderOutput<T = object> {
  value?: string; // An unified way to get the generated value
  usage: {
    inputTokens: number;
    outputTokens: number;
    totalTokens: number;
  };
  inner: T;
}

Error part

// This object represents an error output, where inner is the original error value
export type SessionError<T = object | string> = {
  message: string;
  inner: T;
};

Tested OpenAI compatible providers

missing

  • Anthropic: I don't have free account for it
  • Grok: I don't have free account for it
  • Amazon Bedrock: I don't have free account for it

ideas

  • Gemini: maybe implement a custom provider for this one

- LLM Section is a wrapper to handle LLM inference based on the selected
provider
- Extracting json parsers to a separated file
- Moving LLM stream related code to a separated folder
- Applying LLM provider interfaces to implement the Ollama provider
- Applying LLM provider interfaces to implement the 'openaicompatible'
mode
- Improving Typescript support for dynamic suggestion based on the
selected Session type.
- Break: Now LLM models must be defined inside `options` argument, it
allows a better typescript checking as well makes easier to extend the
API.

- There's no need to check if `inferenceHost` env var is defined, since
we can now switch between different LLM providers. Instead, we can
enable LLM support if the given type is an allowed provider.
- Improving typescript with conditional output types based on the
selected provider
- Defining common properties for LLM providers like `usage` metrics and
simplified `value`
- OpenAI uses a different streaming alternative that ends with `[DONE]`
- Applying 'pattern matching' and 'Result pattern' to improve error
handling. It enforces that users must first check for errors before
consuming the message
- It ensures that only valid strings with content can be embeded
- Fix wrong input variable name.
- Accepting 'opts' param as optinal, applying null safes.
- Improving tests by checking the result types: success or errors
- Testing invalid `gte-small` type name
@kallebysantos kallebysantos marked this pull request as ready for review May 15, 2025 17:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant