Skip to content
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

Tokenize Detokenize compatibility #179

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions examples/tokenization/tokenization.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import ollama from '../../src/browser.js'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
import ollama from '../../src/browser.js'
import ollama from ollama


async function main() {
// Tokenize some text
const tokResponse = await ollama.tokenize({
model: 'llama3.2',
text: 'Why is the sky blue?'
})

console.log('Tokens from model:', tokResponse.tokens)

// Detokenize the tokens back to text
const detokResponse = await ollama.detokenize({
model: 'llama3.2',
tokens: tokResponse.tokens
})

console.log('Text from model:', detokResponse.text)
}

main().catch(console.error)
34 changes: 33 additions & 1 deletion src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
CopyRequest,
CreateRequest,
DeleteRequest,
DetokenizeRequest,
DetokenizeResponse,
EmbedRequest,
EmbedResponse,
EmbeddingsRequest,
Expand All @@ -24,6 +26,8 @@
ShowRequest,
ShowResponse,
StatusResponse,
TokenizeRequest,
TokenizeResponse,
} from './interfaces.js'

export class Ollama {
Expand Down Expand Up @@ -65,7 +69,7 @@
*/
protected async processStreamableRequest<T extends object>(
endpoint: string,
request: { stream?: boolean } & Record<string, any>,

Check warning on line 72 in src/browser.ts

View workflow job for this annotation

GitHub Actions / test (16)

Unexpected any. Specify a different type

Check warning on line 72 in src/browser.ts

View workflow job for this annotation

GitHub Actions / test (18)

Unexpected any. Specify a different type

Check warning on line 72 in src/browser.ts

View workflow job for this annotation

GitHub Actions / test (20)

Unexpected any. Specify a different type
): Promise<T | AbortableAsyncIterator<T>> {
request.stream = request.stream ?? false
const host = `${this.config.host}/api/${endpoint}`
Expand Down Expand Up @@ -322,9 +326,37 @@
})
return (await response.json()) as ListResponse
}
}

/**
* Tokenizes text into tokens.
* @param request {TokenizeRequest} - The request object.
* @returns {Promise<TokenizeResponse>} - The response object.
*/
async tokenize(request: TokenizeRequest): Promise<TokenizeResponse> {
const response = await utils.post(this.fetch, `${this.config.host}/api/tokenize`, {
...request,
}, {
headers: this.config.headers
})
return (await response.json()) as TokenizeResponse
}

/**
* Detokenizes tokens back into text.
* @param request {DetokenizeRequest} - The request object.
* @returns {Promise<DetokenizeResponse>} - The response object.
*/
async detokenize(request: DetokenizeRequest): Promise<DetokenizeResponse> {
const response = await utils.post(this.fetch, `${this.config.host}/api/detokenize`, {
...request,
}, {
headers: this.config.headers
})
return (await response.json()) as DetokenizeResponse
}
}
export default new Ollama()

// export all types from the main entry point so that packages importing types dont need to specify paths
export * from './interfaces.js'

18 changes: 18 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
function: {
name: string;
arguments: {
[key: string]: any;

Check warning on line 74 in src/interfaces.ts

View workflow job for this annotation

GitHub Actions / test (16)

Unexpected any. Specify a different type

Check warning on line 74 in src/interfaces.ts

View workflow job for this annotation

GitHub Actions / test (18)

Unexpected any. Specify a different type

Check warning on line 74 in src/interfaces.ts

View workflow job for this annotation

GitHub Actions / test (20)

Unexpected any. Specify a different type
};
};
}
Expand Down Expand Up @@ -159,6 +159,16 @@
options?: Partial<Options>
}

export interface TokenizeRequest {
model: string
text: string
}

export interface DetokenizeRequest {
model: string
tokens: Int32Array
}

// response types

export interface GenerateResponse {
Expand Down Expand Up @@ -234,14 +244,22 @@
details: ModelDetails
messages: Message[]
modified_at: Date
model_info: Map<string, any>

Check warning on line 247 in src/interfaces.ts

View workflow job for this annotation

GitHub Actions / test (16)

Unexpected any. Specify a different type

Check warning on line 247 in src/interfaces.ts

View workflow job for this annotation

GitHub Actions / test (18)

Unexpected any. Specify a different type

Check warning on line 247 in src/interfaces.ts

View workflow job for this annotation

GitHub Actions / test (20)

Unexpected any. Specify a different type
projector_info?: Map<string, any>

Check warning on line 248 in src/interfaces.ts

View workflow job for this annotation

GitHub Actions / test (16)

Unexpected any. Specify a different type

Check warning on line 248 in src/interfaces.ts

View workflow job for this annotation

GitHub Actions / test (18)

Unexpected any. Specify a different type

Check warning on line 248 in src/interfaces.ts

View workflow job for this annotation

GitHub Actions / test (20)

Unexpected any. Specify a different type
}

export interface ListResponse {
models: ModelResponse[]
}

export interface TokenizeResponse {
tokens: Int32Array
}

export interface DetokenizeResponse {
text: string
}

export interface ErrorResponse {
error: string
}
Expand Down
Loading