| 1 | import ReplicateClient from "replicate"; |
| 2 | |
| 3 | import { CompletionOptions, LLMOptions } from "../../index.js"; |
| 4 | import { BaseLLM } from "../index.js"; |
| 5 | |
| 6 | class Replicate extends BaseLLM { |
| 7 | private static MODEL_IDS: { |
| 8 | [name: string]: `${string}/${string}:${string}`; |
| 9 | } = { |
| 10 | "codellama-7b": |
| 11 | "meta/codellama-7b-instruct:aac3ab196f8a75729aab9368cd45ea6ad3fc793b6cda93b1ded17299df369332", |
| 12 | "codellama-13b": |
| 13 | "meta/codellama-13b-instruct:a5e2d67630195a09b96932f5fa541fe64069c97d40cd0b69cdd91919987d0e7f", |
| 14 | "codellama-34b": |
| 15 | "meta/codellama-34b-instruct:eeb928567781f4e90d2aba57a51baef235de53f907c214a4ab42adabf5bb9736", |
| 16 | "codellama-70b": |
| 17 | "meta/codellama-70b-instruct:a279116fe47a0f65701a8817188601e2fe8f4b9e04a518789655ea7b995851bf", |
| 18 | "llama2-7b": "meta/llama-2-7b-chat" as any, |
| 19 | "llama2-13b": "meta/llama-2-13b-chat" as any, |
| 20 | "llama3-8b": "meta/meta-llama-3-8b-instruct" as any, |
| 21 | "llama3-70b": "meta/meta-llama-3-70b-instruct" as any, |
| 22 | "llama3.1-8b": "meta/meta-llama-3.1-8b-instruct" as any, |
| 23 | "llama3.1-70b": "meta/meta-llama-3.1-70b-instruct" as any, |
| 24 | "llama3.1-405b": "meta/meta-llama-3.1-405b-instruct" as any, |
| 25 | "zephyr-7b": |
| 26 | "nateraw/zephyr-7b-beta:b79f33de5c6c4e34087d44eaea4a9d98ce5d3f3a09522f7328eea0685003a931", |
| 27 | "mistral-7b": |
| 28 | "mistralai/mistral-7b-instruct-v0.1:83b6a56e7c828e667f21fd596c338fd4f0039b46bcfa18d973e8e70e455fda70", |
| 29 | "mistral-8x7b": "mistralai/mixtral-8x7b-instruct-v0.1" as any, |
| 30 | "wizardcoder-34b": |
| 31 | "andreasjansson/wizardcoder-python-34b-v1-gguf:67eed332a5389263b8ede41be3ee7dc119fa984e2bde287814c4abed19a45e54", |
| 32 | "neural-chat-7b": |
| 33 | "tomasmcm/neural-chat-7b-v3-1:acb450496b49e19a1e410b50c574a34acacd54820bc36c19cbfe05148de2ba57", |
| 34 | "deepseek-7b": "kcaverly/deepseek-coder-33b-instruct-gguf" as any, |
| 35 | "phind-codellama-34b": "kcaverly/phind-codellama-34b-v2-gguf" as any, |
| 36 | }; |
| 37 | |
| 38 | static providerName = "replicate"; |
| 39 | private _replicate: ReplicateClient; |
| 40 | |
| 41 | private _convertArgs( |
| 42 | options: CompletionOptions, |
| 43 | prompt: string, |
| 44 | signal: AbortSignal, |
| 45 | ): [`${string}/${string}:${string}`, { input: any; signal: AbortSignal }] { |
| 46 | return [ |
| 47 | Replicate.MODEL_IDS[options.model] || (options.model as any), |
| 48 | { |
| 49 | input: { prompt, message: prompt }, |
| 50 | signal, |
| 51 | }, |
| 52 | ]; |
| 53 | } |
| 54 | |
| 55 | constructor(options: LLMOptions) { |
| 56 | super(options); |
| 57 | this._replicate = new ReplicateClient({ auth: options.apiKey }); |
| 58 | } |
| 59 | |
| 60 | protected async _complete( |
| 61 | prompt: string, |
| 62 | signal: AbortSignal, |
| 63 | options: CompletionOptions, |
| 64 | ): Promise<string> { |
| 65 | const [model, args] = this._convertArgs(options, prompt, signal); |
| 66 | const response = await this._replicate.run(model, args); |
| 67 | |
| 68 | return (response as any)[0]; |
| 69 | } |
| 70 | |
| 71 | protected async *_streamComplete( |
| 72 | prompt: string, |
| 73 | signal: AbortSignal, |
| 74 | options: CompletionOptions, |
| 75 | ): AsyncGenerator<string> { |
| 76 | const [model, args] = this._convertArgs(options, prompt, signal); |
| 77 | for await (const event of this._replicate.stream(model, args)) { |
| 78 | if (event.event === "output") { |
| 79 | yield event.data; |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | export default Replicate; |