Skip to content

Commit

Permalink
Add OpenAI-compatible completion model to facade.
Browse files Browse the repository at this point in the history
  • Loading branch information
lgrammel committed Dec 30, 2023
1 parent 6c06e2a commit 4d86148
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import dotenv from "dotenv";
import {
FireworksAIApiConfiguration,
openaicompatible,
streamText,
} from "modelfusion";

dotenv.config();

async function main() {
const textStream = await streamText(
openaicompatible.CompletionTextGenerator({
api: new FireworksAIApiConfiguration(),
model: "accounts/fireworks/models/mistral-7b",
maxGenerationTokens: 500,
}),

"Write a story about a robot learning to love"
);

for await (const textPart of textStream) {
process.stdout.write(textPart);
}
}

main().catch(console.error);
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,35 @@ import {
OpenAICompatibleChatModel,
OpenAICompatibleChatSettings,
} from "./OpenAICompatibleChatModel.js";
import { OpenAICompatibleCompletionModel } from "./OpenAICompatibleCompletionModel.js";

/**
* Create a text generation model that calls an API that is compatible with OpenAI's completion API.
*
* Please note that many providers implement the API with slight differences, which can cause
* unexpected errors and different behavior in less common scenarios.
*
* @see https://platform.openai.com/docs/api-reference/completions/create
*
* @example
* ```ts
* const model = openaicompatible.CompletionTextGenerator({
* model: "provider-specific-model-name",
* temperature: 0.7,
* maxGenerationTokens: 500,
* });
*
* const text = await generateText(
* model,
* "Write a short story about a robot learning to love:"
* );
* ```
*/
export function CompletionTextGenerator(
settings: OpenAICompatibleChatSettings
) {
return new OpenAICompatibleCompletionModel(settings);
}

/**
* Create a text generation model that calls an API that is compatible with OpenAI's chat API.
Expand All @@ -12,18 +41,22 @@ import {
* @see https://platform.openai.com/docs/api-reference/chat/create
*
* @example
* ```ts
* const model = openaicompatible.ChatTextGenerator({
* model: "provider-specific-model-name",
* temperature: 0.7,
* maxGenerationTokens: 500,
* });
*
* const text = await generateText([
* const text = await generateText(
* model,
* openai.ChatMessage.system(
* "Write a short story about a robot learning to love:"
* ),
* ]);
* [
* openai.ChatMessage.user(
* "Write a short story about a robot learning to love:"
* ),
* ]
* );
* ```
*/
export function ChatTextGenerator(settings: OpenAICompatibleChatSettings) {
return new OpenAICompatibleChatModel(settings);
Expand Down

1 comment on commit 4d86148

@vercel
Copy link

@vercel vercel bot commented on 4d86148 Dec 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.