Skip to content

Commit

Permalink
feat(docs): update
Browse files Browse the repository at this point in the history
  • Loading branch information
jhen0409 committed Jul 28, 2024
1 parent ad7e0a5 commit ad10d2f
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 78 deletions.
114 changes: 75 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ You can search HuggingFace for available models (Keyword: [`GGUF`](https://huggi
For create a GGUF model manually, for example in Llama 2:

Download the Llama 2 model

1. Request access from [here](https://ai.meta.com/llama)
2. Download the model from HuggingFace [here](https://huggingface.co/meta-llama/Llama-2-7b-chat) (`Llama-2-7b-chat`)

Convert the model to ggml format

```bash
# Start with submodule in this repo (or you can clone the repo https://github.com/ggerganov/llama.cpp.git)
yarn && yarn bootstrap
Expand Down Expand Up @@ -76,26 +78,53 @@ const context = await initLlama({
// embedding: true, // use embedding
})

// Do completion
const { text, timings } = await context.completion(
const stopWords = ['</s>', '<|end|>', '<|eot_id|>', '<|end_of_text|>', '<|im_end|>', '<|EOT|>', '<|END_OF_TURN_TOKEN|>', '<|end_of_turn|>', '<|endoftext|>']

// Do chat completion
const msgResult = await context.completion(
{
messages: [
{
role: 'system',
content: 'This is a conversation between user and assistant, a friendly chatbot.',
},
{
role: 'user,
content: 'Hello!',
},
],
n_predict: 100,
stop: stopWords,
// ...other params
},
(data) => {
// This is a partial completion callback
const { token } = data
},
)
console.log('Result:', msgResult.text)
console.log('Timings:', msgResult.timings)
// Or do text completion
const textResult = await context.completion(
{
prompt: 'This is a conversation between user and llama, a friendly chatbot. respond in simple markdown.\n\nUser: Hello!\nLlama:',
n_predict: 100,
stop: ['</s>', 'Llama:', 'User:'],
// n_threads: 4,
stop: [...stopWords, 'Llama:', 'User:'],
// ...other params
},
(data) => {
// This is a partial completion callback
const { token } = data
},
)
console.log('Result:', text)
console.log('Timings:', timings)
console.log('Result:', textResult.text)
console.log('Timings:', textResult.timings)
```
The binding’s deisgn inspired by [server.cpp](https://github.com/ggerganov/llama.cpp/tree/master/examples/server) example in llama.cpp, so you can map its API to LlamaContext:
- `/completion`: `context.completion(params, partialCompletionCallback)`
- `/completion` and `/chat/completions`: `context.completion(params, partialCompletionCallback)`
- `/tokenize`: `context.tokenize(content)`
- `/detokenize`: `context.detokenize(tokens)`
- `/embedding`: `context.embedding(content)`
Expand All @@ -110,6 +139,7 @@ Please visit the [Documentation](docs/API) for more details.
You can also visit the [example](example) to see how to use it.
Run the example:
```bash
yarn && yarn bootstrap
Expand Down Expand Up @@ -142,7 +172,9 @@ You can see [GBNF Guide](https://github.com/ggerganov/llama.cpp/tree/master/gram
```js
import { initLlama, convertJsonSchemaToGrammar } from 'llama.rn'
const schema = { /* JSON Schema, see below */ }
const schema = {
/* JSON Schema, see below */
}
const context = await initLlama({
model: 'file://<path to gguf model>',
Expand All @@ -153,7 +185,7 @@ const context = await initLlama({
grammar: convertJsonSchemaToGrammar({
schema,
propOrder: { function: 0, arguments: 1 },
})
}),
})

const { text } = await context.completion({
Expand All @@ -171,80 +203,81 @@ console.log('Result:', text)
{
oneOf: [
{
type: "object",
name: "get_current_weather",
description: "Get the current weather in a given location",
type: 'object',
name: 'get_current_weather',
description: 'Get the current weather in a given location',
properties: {
function: {
const: "get_current_weather",
const: 'get_current_weather',
},
arguments: {
type: "object",
type: 'object',
properties: {
location: {
type: "string",
description: "The city and state, e.g. San Francisco, CA",
type: 'string',
description: 'The city and state, e.g. San Francisco, CA',
},
unit: {
type: "string",
enum: ["celsius", "fahrenheit"],
type: 'string',
enum: ['celsius', 'fahrenheit'],
},
},
required: ["location"],
required: ['location'],
},
},
},
{
type: "object",
name: "create_event",
description: "Create a calendar event",
type: 'object',
name: 'create_event',
description: 'Create a calendar event',
properties: {
function: {
const: "create_event",
const: 'create_event',
},
arguments: {
type: "object",
type: 'object',
properties: {
title: {
type: "string",
description: "The title of the event",
type: 'string',
description: 'The title of the event',
},
date: {
type: "string",
description: "The date of the event",
type: 'string',
description: 'The date of the event',
},
time: {
type: "string",
description: "The time of the event",
type: 'string',
description: 'The time of the event',
},
},
required: ["title", "date", "time"],
required: ['title', 'date', 'time'],
},
},
},
{
type: "object",
name: "image_search",
description: "Search for an image",
type: 'object',
name: 'image_search',
description: 'Search for an image',
properties: {
function: {
const: "image_search",
const: 'image_search',
},
arguments: {
type: "object",
type: 'object',
properties: {
query: {
type: "string",
description: "The search query",
type: 'string',
description: 'The search query',
},
},
required: ["query"],
required: ['query'],
},
},
},
],
}
```
</details>
<details>
Expand All @@ -268,6 +301,7 @@ string ::= "\"" (
2 ::= "{" space "\"function\"" space ":" space 2-function "," space "\"arguments\"" space ":" space 2-arguments "}" space
root ::= 0 | 1 | 2
```

</details>

## Mock `llama.rn`
Expand All @@ -281,12 +315,14 @@ jest.mock('llama.rn', () => require('llama.rn/jest/mock'))
## NOTE

iOS:

- The [Extended Virtual Addressing](https://developer.apple.com/documentation/bundleresources/entitlements/com_apple_developer_kernel_extended-virtual-addressing) capability is recommended to enable on iOS project.
- Metal:
- We have tested to know some devices is not able to use Metal ('params.n_gpu_layers > 0') due to llama.cpp used SIMD-scoped operation, you can check if your device is supported in [Metal feature set tables](https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf), Apple7 GPU will be the minimum requirement.
- It's also not supported in iOS simulator due to [this limitation](https://developer.apple.com/documentation/metal/developing_metal_apps_that_run_in_simulator#3241609), we used constant buffers more than 14.

Android:

- Currently only supported arm64-v8a / x86_64 platform, this means you can't initialize a context on another platforms. The 64-bit platform are recommended because it can allocate more memory for the model.
- No integrated any GPU backend yet.

Expand Down
18 changes: 9 additions & 9 deletions docs/API/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ llama.rn

#### Defined in

[index.ts:43](https://github.com/mybigday/llama.rn/blob/f95f600/src/index.ts#L43)
[index.ts:51](https://github.com/mybigday/llama.rn/blob/ad7e0a5/src/index.ts#L51)

___

### CompletionParams

Ƭ **CompletionParams**: `Omit`<`NativeCompletionParams`, ``"emit_partial_completion"``\>
Ƭ **CompletionParams**: `Omit`<`NativeCompletionParams`, ``"emit_partial_completion"`` \| ``"prompt"``\> & { `messages?`: `RNLlamaOAICompatibleMessage`[] ; `prompt?`: `string` }

#### Defined in

[index.ts:41](https://github.com/mybigday/llama.rn/blob/f95f600/src/index.ts#L41)
[index.ts:43](https://github.com/mybigday/llama.rn/blob/ad7e0a5/src/index.ts#L43)

___

Expand All @@ -63,7 +63,7 @@ ___

#### Defined in

[index.ts:39](https://github.com/mybigday/llama.rn/blob/f95f600/src/index.ts#L39)
[index.ts:41](https://github.com/mybigday/llama.rn/blob/ad7e0a5/src/index.ts#L41)

___

Expand All @@ -80,7 +80,7 @@ ___

#### Defined in

[index.ts:29](https://github.com/mybigday/llama.rn/blob/f95f600/src/index.ts#L29)
[index.ts:31](https://github.com/mybigday/llama.rn/blob/ad7e0a5/src/index.ts#L31)

## Functions

Expand All @@ -104,7 +104,7 @@ ___

#### Defined in

[grammar.ts:824](https://github.com/mybigday/llama.rn/blob/f95f600/src/grammar.ts#L824)
[grammar.ts:824](https://github.com/mybigday/llama.rn/blob/ad7e0a5/src/grammar.ts#L824)

___

Expand All @@ -124,7 +124,7 @@ ___

#### Defined in

[index.ts:166](https://github.com/mybigday/llama.rn/blob/f95f600/src/index.ts#L166)
[index.ts:191](https://github.com/mybigday/llama.rn/blob/ad7e0a5/src/index.ts#L191)

___

Expand All @@ -138,7 +138,7 @@ ___

#### Defined in

[index.ts:182](https://github.com/mybigday/llama.rn/blob/f95f600/src/index.ts#L182)
[index.ts:211](https://github.com/mybigday/llama.rn/blob/ad7e0a5/src/index.ts#L211)

___

Expand All @@ -158,4 +158,4 @@ ___

#### Defined in

[index.ts:162](https://github.com/mybigday/llama.rn/blob/f95f600/src/index.ts#L162)
[index.ts:187](https://github.com/mybigday/llama.rn/blob/ad7e0a5/src/index.ts#L187)
Loading

0 comments on commit ad10d2f

Please sign in to comment.