Skip to content

Commit

Permalink
wip: fix generate_completion params
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoh86 committed Jan 9, 2024
1 parent ce403e9 commit 14be818
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 38 deletions.
53 changes: 24 additions & 29 deletions denops/ollama/api/generate_completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
is,
type PredicateType,
} from "https://deno.land/x/[email protected]/mod.ts";
import { isErrorResponse, isFormat, type RequestOptions } from "./types.ts";
import { isErrorResponse, type RequestOptions } from "./types.ts";
import { parseJSONStream } from "./base.ts";
import { doPost } from "./base.ts";

Expand All @@ -11,32 +11,6 @@ import { doPost } from "./base.ts";
// Endpoint: /api/generate
// Usage: https://github.com/jmorganca/ollama/blob/main/docs/api.md#generate-a-completion

export const isGenerateCompletionParam = is.ObjectOf({
// The model name
model: is.String,
// The prompt to generate a response for
prompt: is.String,
// (optional) A list of base64-encoded images (for multimodal models such as llava)
images: is.OptionalOf(is.ArrayOf(is.String)),
// (optional) The format to return a response in. Currently the only accepted value is json
format: isFormat,
// (optional) Additional model parameters listed in the documentation for the Modelfile such as temperature
options: is.OptionalOf(is.Record),
// (optional) System message to (overrides what is defined in the Modelfile)
system: is.OptionalOf(is.String),
// (optional) The full prompt or prompt template (overrides what is defined in the Modelfile)
template: is.OptionalOf(is.String),
// (optional) The context parameter returned from a previous request to /generate, this can be used to keep a short conversational memory
context: is.OptionalOf(is.ArrayOf(is.Number)),
// (optional) If false the response will be returned as a single response object, rather than a stream of objects
stream: is.OptionalOf(is.Boolean),
// (optional) If true no formatting will be applied to the prompt. You may choose to use the raw parameter if you are specifying a full templated prompt in your request to the API.
raw: is.OptionalOf(is.Boolean),
});
export type GenerateCompletionParam = PredicateType<
typeof isGenerateCompletionParam
>;

export const isGenerateCompletionResponse = is.OneOf([
isErrorResponse,
is.ObjectOf({
Expand Down Expand Up @@ -68,16 +42,37 @@ export type GenerateCompletionResponse = PredicateType<
typeof isGenerateCompletionResponse
>;

export type GenerateCompletionParam = {
// A list of base64-encoded images (for multimodal models such as llava)
images?: string[] | undefined;
// The format to return a response in. Currently the only accepted value is json
format?: "json" | undefined;
// Additional model parameters listed in the documentation for the Modelfile such as temperature
options?: Record<PropertyKey, unknown> | undefined;
// System message to (overrides what is defined in the Modelfile)
system?: string | undefined;
// The full prompt or prompt template (overrides what is defined in the Modelfile)
template?: string | undefined;
// The context parameter returned from a previous request to /generate, this can be used to keep a short conversational memory
context?: number[] | undefined;
// If true no formatting will be applied to the prompt. You may choose to use the raw parameter if you are specifying a full templated prompt in your request to the API.
raw?: boolean | undefined;
};

/** Generate a response for a given prompt with a provided model.
* This is a streaming endpoint, so there will be a series of responses.
* The final response object will include statistics and additional data from the request.
*/
export async function generateCompletion(
param: GenerateCompletionParam,
// The model name
model: string,
// The prompt to generate a response for
prompt: string,
param?: GenerateCompletionParam,
options?: RequestOptions,
) {
return parseJSONStream(
await doPost("/api/generate", param, options),
await doPost("/api/generate", { model, prompt, ...param }, options),
isGenerateCompletionResponse,
);
}
15 changes: 7 additions & 8 deletions denops/ollama/api/generate_completion_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ Deno.test("generateCompletion", async (t) => {
});
try {
await t.step("should call fetch with the correct arguments", async () => {
const result = await generateCompletion({
model: "model1",
prompt: "How to run?",
});
const result = await generateCompletion(
"model1",
"How to run?",
);
assertEquals(result.response.status, 200);
assertSpyCalls(fetchStub, 1);
assertSpyCallArgs(fetchStub, 0, [
Expand Down Expand Up @@ -76,14 +76,13 @@ Deno.test("generateCompletion", async (t) => {
try {
await t.step("should call fetch with the correct arguments", async () => {
const result = await generateCompletion(
"model1",
"run",
{
model: "model1",
prompt: "run",
context: [1, 2, 3],
format: "json",
images: ["foo-image-1", "foo-image-2"],
system: "foo-system",
stream: true,
raw: true,
template: "How to %s?",
},
Expand All @@ -97,7 +96,7 @@ Deno.test("generateCompletion", async (t) => {
new URL("https://example.com:33562/api/generate"),
{
body:
'{"model":"model1","prompt":"run","context":[1,2,3],"format":"json","images":["foo-image-1","foo-image-2"],"system":"foo-system","stream":true,"raw":true,"template":"How to %s?"}',
'{"model":"model1","prompt":"run","context":[1,2,3],"format":"json","images":["foo-image-1","foo-image-2"],"system":"foo-system","raw":true,"template":"How to %s?"}',
headers: { "Content-Type": "application/json" },
method: "POST",
},
Expand Down
2 changes: 1 addition & 1 deletion denops/ollama/dispatch/start_chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ async function promptCallback(

const { signal, cancel } = await canceller(denops);
try {
const result = await generateCompletion({ model, prompt, context }, {
const result = await generateCompletion(model, prompt, { context }, {
signal,
});
if (!result.body) {
Expand Down

0 comments on commit 14be818

Please sign in to comment.