mirror of
https://github.com/geoffsee/open-gsio.git
synced 2025-09-08 22:56:46 +00:00

- Moved `providers`, `services`, `models`, `lib`, and related files to `src` directory within `server` package. - Adjusted imports across the codebase to reflect the new paths. - Renamed several `.ts` files for consistency. - Introduced an `index.ts` in the `ai/providers` package to export all providers. This improves maintainability and aligns with the project's updated directory structure.
75 lines
1.8 KiB
TypeScript
75 lines
1.8 KiB
TypeScript
import { OpenAI } from 'openai';
|
|
|
|
import { BaseChatProvider, CommonProviderParams } from './chat-stream-provider.ts';
|
|
|
|
export class XaiChatProvider extends BaseChatProvider {
|
|
getOpenAIClient(param: CommonProviderParams): OpenAI {
|
|
return new OpenAI({
|
|
baseURL: 'https://api.x.ai/v1',
|
|
apiKey: param.env.XAI_API_KEY,
|
|
});
|
|
}
|
|
|
|
getStreamParams(param: CommonProviderParams, safeMessages: any[]): any {
|
|
const tuningParams = {
|
|
temperature: 0.75,
|
|
};
|
|
|
|
const getTuningParams = () => {
|
|
return tuningParams;
|
|
};
|
|
|
|
return {
|
|
model: param.model,
|
|
messages: safeMessages,
|
|
stream: true,
|
|
...getTuningParams(),
|
|
};
|
|
}
|
|
|
|
async processChunk(chunk: any, dataCallback: (data: any) => void): Promise<boolean> {
|
|
if (chunk.choices && chunk.choices[0]?.finish_reason === 'stop') {
|
|
dataCallback({ type: 'chat', data: chunk });
|
|
return true;
|
|
}
|
|
|
|
dataCallback({ type: 'chat', data: chunk });
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export class XaiChatSdk {
|
|
private static provider = new XaiChatProvider();
|
|
|
|
static async handleXaiStream(
|
|
ctx: {
|
|
openai: OpenAI;
|
|
systemPrompt: any;
|
|
preprocessedContext: any;
|
|
maxTokens: unknown | number | undefined;
|
|
messages: any;
|
|
disableWebhookGeneration: boolean;
|
|
model: any;
|
|
env: Env;
|
|
},
|
|
dataCallback: (data: any) => any,
|
|
) {
|
|
if (!ctx.messages?.length) {
|
|
return new Response('No messages provided', { status: 400 });
|
|
}
|
|
|
|
return this.provider.handleStream(
|
|
{
|
|
systemPrompt: ctx.systemPrompt,
|
|
preprocessedContext: ctx.preprocessedContext,
|
|
maxTokens: ctx.maxTokens,
|
|
messages: ctx.messages,
|
|
model: ctx.model,
|
|
env: ctx.env,
|
|
disableWebhookGeneration: ctx.disableWebhookGeneration,
|
|
},
|
|
dataCallback,
|
|
);
|
|
}
|
|
}
|