feat: OpenRouter Support & Improve Model Fetching ⇆ (#936)

* chore(ChatGPTClient.js): add support for OpenRouter API
chore(OpenAIClient.js): add support for OpenRouter API

* chore: comment out token debugging

* chore: add back streamResult assignment

* chore: remove double condition/assignment from merging

* refactor(routes/endpoints): -> controller/services logic

* feat: add openrouter model fetching

* chore: remove unused endpointsConfig in cleanupPreset function

* refactor: separate models concern from endpointsConfig

* refactor(data-provider): add TModels type and make TEndpointsConfig adaptible to new endpoint keys

* refactor: complete models endpoint service in data-provider

* refactor: onMutate for refreshToken and login, invalidate models query

* feat: complete models endpoint logic for frontend

* chore: remove requireJwtAuth from /api/endpoints and /api/models as not implemented yet

* fix: endpoint will not be overwritten and instead use active value

* feat: openrouter support for plugins

* chore(EndpointOptionsDialog): remove unused recoil value

* refactor(schemas/parseConvo): add handling of secondaryModels to use first of defined secondary models, which includes last selected one as first, or default to the convo's secondary model value

* refactor: remove hooks from store and move to hooks
refactor(switchToConversation): make switchToConversation use latest recoil state, which is necessary to get the most up-to-date models list, replace wrapper function
refactor(getDefaultConversation): factor out logic into 3 pieces to reduce complexity.

* fix: backend tests

* feat: optimistic update by calling newConvo when models are fetched

* feat: openrouter support for titling convos

* feat: cache models fetch

* chore: add missing dep to AuthContext useEffect

* chore: fix useTimeout types

* chore: delete old getDefaultConvo file

* chore: remove newConvo logic from Root, remove console log from api models caching

* chore: ensure bun is used for building in b:client script

* fix: default endpoint will not default to null on a completely fresh login (no localStorage/cookies)

* chore: add openrouter docs to free_ai_apis.md and .env.example

* chore: remove openrouter console logs

* feat: add debugging env variable for Plugins
This commit is contained in:
Danny Avila 2023-09-18 12:55:51 -04:00 committed by GitHub
parent ccb46164c0
commit fd70e21732
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
58 changed files with 809 additions and 523 deletions

View file

@ -36,6 +36,8 @@ export const deletePreset = () => '/api/presets/delete';
export const aiEndpoints = () => '/api/endpoints';
export const models = () => '/api/models';
export const tokenizer = () => '/api/tokenizer';
export const login = () => '/api/auth/login';

View file

@ -1,5 +1,7 @@
import * as t from './types';
import * as s from './schemas';
/* TODO: fix dependency cycle */
// eslint-disable-next-line import/no-cycle
import request from './request';
import * as endpoints from './api-endpoints';
@ -99,6 +101,10 @@ export const getAIEndpoints = () => {
return request.get(endpoints.aiEndpoints());
};
export const getModels = () => {
return request.get(endpoints.models());
};
export const updateTokenCount = (text: string) => {
return request.post(endpoints.tokenizer(), { arg: text });
};

View file

@ -17,6 +17,7 @@ export enum QueryKeys {
searchEnabled = 'searchEnabled',
user = 'user',
name = 'name', // user key name
models = 'models',
endpoints = 'endpoints',
presets = 'presets',
searchResults = 'searchResults',
@ -218,6 +219,14 @@ export const useGetEndpointsQuery = (): QueryObserverResult<t.TEndpointsConfig>
});
};
export const useGetModelsQuery = (): QueryObserverResult<t.TModelsConfig> => {
return useQuery([QueryKeys.models], () => dataService.getModels(), {
refetchOnWindowFocus: false,
refetchOnReconnect: false,
refetchOnMount: false,
});
};
export const useCreatePresetMutation = (): UseMutationResult<
s.TPreset[],
unknown,
@ -313,6 +322,9 @@ export const useLoginUserMutation = (): UseMutationResult<
onSuccess: () => {
queryClient.invalidateQueries([QueryKeys.user]);
},
onMutate: () => {
queryClient.invalidateQueries([QueryKeys.models]);
},
});
};
@ -345,7 +357,12 @@ export const useRefreshTokenMutation = (): UseMutationResult<
unknown,
unknown
> => {
return useMutation(() => dataService.refreshToken(), {});
const queryClient = useQueryClient();
return useMutation(() => dataService.refreshToken(), {
onMutate: () => {
queryClient.invalidateQueries([QueryKeys.models]);
},
});
};
export const useUserKeyQuery = (

View file

@ -1,5 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import axios, { AxiosRequestConfig, AxiosError } from 'axios';
/* TODO: fix dependency cycle */
// eslint-disable-next-line import/no-cycle
import { refreshToken } from './data-service';
import { setTokenHeader } from './headers-helpers';

View file

@ -369,7 +369,8 @@ function getFirstDefinedValue(possibleValues: string[]) {
}
type TPossibleValues = {
model: string[];
models: string[];
secondaryModels?: string[];
};
export const parseConvo = (
@ -383,10 +384,15 @@ export const parseConvo = (
throw new Error(`Unknown endpoint: ${endpoint}`);
}
const convo = schema.parse(conversation);
const convo = schema.parse(conversation) as TConversation;
const { models, secondaryModels } = possibleValues ?? {};
if (possibleValues && convo) {
convo.model = getFirstDefinedValue(possibleValues.model) ?? convo.model;
if (models && convo) {
convo.model = getFirstDefinedValue(models) ?? convo.model;
}
if (secondaryModels && convo.agentOptions) {
convo.agentOptions.model = getFirstDefinedValue(secondaryModels) ?? convo.agentOptions.model;
}
return convo;

View file

@ -111,21 +111,16 @@ export type TSearchResults = {
};
export type TConfig = {
availableModels: [];
availableModels?: [];
userProvide?: boolean | null;
availableTools?: [];
plugins?: [];
azure?: boolean;
};
export type TEndpointsConfig = {
azureOpenAI: TConfig | null;
bingAI: TConfig | null;
chatGPTBrowser: TConfig | null;
anthropic: TConfig | null;
google: TConfig | null;
openAI: TConfig | null;
gptPlugins: TConfig | null;
};
export type TModelsConfig = Record<string, string[]>;
export type TEndpointsConfig = Record<string, TConfig | null>;
export type TUpdateTokenCountResponse = {
count: number;