|
| 1 | +# SDKs |
| 2 | + |
| 3 | +Currently, there are three SDKs being maintained: [Python](https://github.com/gptscript-ai/py-gptscript), [Node](https://github.com/gptscript-ai/node-gptscript), and [Go](https://github.com/gptscript-ai/go-gptscript). |
| 4 | + |
| 5 | +The goal with these SDKs is to have the same options and functionality as the `gptscript` CLI through language bindings. If you find that some functionality is missing or different from the CLI, please open an issue in the corresponding SDK repo. |
| 6 | + |
| 7 | +Below are some simple examples for each to get started. However, the individual repos will have more comprehensive examples, and include all currently supported functionality. Additionally, the individual repos contain installation instructions and required dependencies. |
| 8 | + |
| 9 | +### Python |
| 10 | + |
| 11 | +The [Python SDK](https://github.com/gptscript-ai/py-gptscript) includes a `Tool` data structure corresponding to the documented [tool reference](07-gpt-file-reference#tool-parameters), which can be used to construct and run tools. |
| 12 | + |
| 13 | +```python |
| 14 | +from gptscript.command import exec |
| 15 | +from gptscript.tool import Tool |
| 16 | + |
| 17 | +tool = Tool( |
| 18 | + json_response=True, |
| 19 | + instructions=""" |
| 20 | +Create three short graphic artist descriptions and their muses. |
| 21 | +These should be descriptive and explain their point of view. |
| 22 | +Also come up with a made up name, they each should be from different |
| 23 | +backgrounds and approach art differently. |
| 24 | +the response should be in JSON and match the format: |
| 25 | +{ |
| 26 | + artists: [{ |
| 27 | + name: "name" |
| 28 | + description: "description" |
| 29 | + }] |
| 30 | +} |
| 31 | +""", |
| 32 | + ) |
| 33 | + |
| 34 | + |
| 35 | +response = exec(tool) |
| 36 | +print(response) |
| 37 | +``` |
| 38 | + |
| 39 | +The SDK also supports running GPTScripts from files. |
| 40 | + |
| 41 | +```python |
| 42 | +from gptscript.command import exec_file |
| 43 | + |
| 44 | +response = exec_file("./example.gpt") |
| 45 | +print(response) |
| 46 | +``` |
| 47 | + |
| 48 | +For more functionality, like streaming output and the supported options, visit the [repo](https://github.com/gptscript-ai/py-gptscript). |
| 49 | + |
| 50 | +### Node |
| 51 | + |
| 52 | +The [Node SDK](https://github.com/gptscript-ai/node-gptscript) includes a `Tool` data structure corresponding to the documented [tool reference](07-gpt-file-reference#tool-parameters), which can be used to construct and run tools. |
| 53 | + |
| 54 | +```javascript |
| 55 | +const gptscript = require('@gptscript-ai/gptscript'); |
| 56 | + |
| 57 | +const t = new gptscript.Tool({ |
| 58 | + instructions: "who was the president of the united states in 1928?" |
| 59 | +}); |
| 60 | + |
| 61 | +try { |
| 62 | + const response = await gptscript.exec(t); |
| 63 | + console.log(response); |
| 64 | +} catch (error) { |
| 65 | + console.error(error); |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +The SDK also supports running GPTScripts from files. |
| 70 | + |
| 71 | +```javascript |
| 72 | +const gptscript = require('@gptscript-ai/gptscript'); |
| 73 | + |
| 74 | +const opts = { |
| 75 | + cache: false, |
| 76 | +}; |
| 77 | + |
| 78 | +async function execFile() { |
| 79 | + try { |
| 80 | + const out = await foo.execFile('./hello.gpt', "--input World", opts); |
| 81 | + console.log(out); |
| 82 | + } catch (e) { |
| 83 | + console.error(e); |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +For more functionality, like streaming output and the supported options, visit the [repo](https://github.com/gptscript-ai/node-gptscript). |
| 89 | + |
| 90 | +### Go |
| 91 | + |
| 92 | +The [Go SDK](https://github.com/gptscript-ai/go-gptscript) includes a `Tool` data structure corresponding to the documented [tool reference](07-gpt-file-reference#tool-parameters), which can be used to construct and run tools. |
| 93 | + |
| 94 | +```go |
| 95 | +package main |
| 96 | + |
| 97 | +import ( |
| 98 | + "context" |
| 99 | + |
| 100 | + gptscript "github.com/gptscript-ai/go-gptscript" |
| 101 | +) |
| 102 | + |
| 103 | +func runTool(ctx context.Context) (string, error) { |
| 104 | + t := gptscript.Tool{ |
| 105 | + Instructions: "Who was the president of the united states in 1928?", |
| 106 | + } |
| 107 | + |
| 108 | + return gptscript.ExecTool(ctx, gptscript.Opts{}, t) |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +The SDK also supports running GPTScripts from files. |
| 113 | + |
| 114 | +```go |
| 115 | +package main |
| 116 | + |
| 117 | +import ( |
| 118 | + "context" |
| 119 | + |
| 120 | + gptscript "github.com/gptscript-ai/go-gptscript" |
| 121 | +) |
| 122 | + |
| 123 | +func execFile(ctx context.Context) (string, error) { |
| 124 | + opts := gptscript.Opts{ |
| 125 | + DisableCache: &[]bool{true}[0], |
| 126 | + } |
| 127 | + |
| 128 | + return gptscript.ExecFile(ctx, "./hello.gpt", "--input World", opts) |
| 129 | +} |
| 130 | +``` |
| 131 | + |
| 132 | +For more functionality, like streaming output and the supported options, visit the [repo](https://github.com/gptscript-ai/go-gptscript). |
0 commit comments