Vals using openai
OpenAI - Docs ↗
Use OpenAI's chat completion API with std/openai
. This integration enables access to OpenAI's language models without needing to acquire API keys.
Streaming is not yet supported. Upvote the HTTP response streaming feature request if you need it!
Usage
Create valimport { OpenAI } from "https://esm.town/v/std/openai";
const openai = new OpenAI();
const completion = await openai.chat.completions.create({
messages: [
{ role: "user", content: "Say hello in a creative way" },
],
model: "gpt-4",
max_tokens: 30,
});
console.log(completion.choices[0].message.content);
Limits
While our wrapper simplifies the integration of OpenAI, there are a few limitations to keep in mind:
- Usage Quota: We limit each user to 10 requests per minute.
- Features: Chat completions is the only endpoint available.
If these limits are too low, let us know! You can also get around the limitation by using your own keys:
- Create your own API key on OpenAI's website
- Create an environment variable named
OPENAI_API_KEY
- Use the
OpenAI
client fromnpm:openai
:
OpenAI
Get started using OpenAI's chat completion without the need to set your own API keys.
Usage
Here's a quick example to get you started with the Val Town OpenAI wrapper:
Create valimport { OpenAI } from "https://esm.town/v/std/openai";
const openai = new OpenAI();
const functionExpression = await openai.chat.completions.create({
"messages": [
{ "role": "user", "content": "Say hello in a creative way" },
],
model: "gpt-4",
max_tokens: 30,
});
console.log(functionExpression.choices[0].message.content);
OpenAI - Docs ↗
Use OpenAI's chat completion API with std/openai
. This integration enables access to OpenAI's language models without needing to acquire API keys.
For free Val Town users, all calls are sent to gpt-3.5-turbo
.
Streaming is not yet supported. Upvote the HTTP response streaming feature request if you need it!
Usage
Create valimport { OpenAI } from "https://esm.town/v/std/openai";
const openai = new OpenAI();
const completion = await openai.chat.completions.create({
messages: [
{ role: "user", content: "Say hello in a creative way" },
],
model: "gpt-4",
max_tokens: 30,
});
console.log(completion.choices[0].message.content);
Limits
While our wrapper simplifies the integration of OpenAI, there are a few limitations to keep in mind:
- Usage Quota: We limit each user to 10 requests per minute.
- Features: Chat completions is the only endpoint available.
If these limits are too low, let us know! You can also get around the limitation by using your own keys:
- Create your own API key on OpenAI's website
- Create an environment variable named
OPENAI_API_KEY
- Use the
OpenAI
client fromnpm:openai
:
If you fork this, you'll need to set OPENAI_API_KEY
in your Val Town Secrets.
If you fork this, you'll need to set OPENAI_API_KEY
in your Val Town Secrets.
GPT4 Example
This uses the brand new gpt-4-1106-preview
.
To use this, set OPENAI_API_KEY
in your Val Town Secrets.
Code Documentation Assistant
The Code Documentation Assistant is an AI-powered tool that helps generate documentation for code. It uses the OpenAI GPT-3.5 Turbo model to generate readme files in GitHub-flavored markdown based on the provided code.
Usage
Importing the Code Documentation Assistant
import { draftReadme, writeReadme } from "code-doc-assistant";
Function: draftReadme
async function draftReadme(options: WriterOptions): Promise<string>
The draftReadme
function generates a readme file based on the provided options.
Parameters
options
(required): An object containing the following properties:username
(string): The username of the code owner.valName
(string): The name of the Val containing the code.model
(optional, default: "gpt-3.5-turbo"): The OpenAI model to use for generating the readme.userPrompt
(optional): Additional prompt to include in the documentation.
Return Value
A promise that resolves to a string representing the generated readme file.
Function: writeReadme
async function writeReadme(options: WriterOptions): Promise<string>
The writeReadme
function generates a readme file and updates the readme of the corresponding Val with the generated content.
Parameters
options
(required): An object containing the following properties:username
(string): The username of the code owner.valName
(string): The name of the Val containing the code.model
(optional, default: "gpt-3.5-turbo"): The OpenAI model to use for generating the readme.userPrompt
(optional): Additional prompt to include in the documentation.
Return Value
A promise that resolves to a string indicating the success of the readme update.
Example
import { draftReadme, writeReadme } from "code-doc-assistant";
const options = {
username: "your-username",
valName: "your-val-name",
};
const generatedReadme = await draftReadme(options);
console.log(generatedReadme);
const successMessage = await writeReadme(options);
console.log(successMessage);
License
This project is licensed under the MIT License.
Val Town AI Readme Writer
This val provides a class ReadmeWriter
for generating readmes for vals with OpenAI. It can both draft readmes and update them directly
PRs welcome! See Todos below for some ideas I have.
Usage
To draft a readme for a given code, use the draftReadme
method:
import { ReadmeWriter } from "https://esm.town/v/nbbaier/readmeGPT";
const readmeWriter = new ReadmeWriter({});
const val = "https://www.val.town/v/:username/:valname";
const generatedReadme = await readmeWriter.draftReadme(val);
To write and update a readme for a given code, use the writeReadme
method:
import { ReadmeWriter } from "https://esm.town/v/nbbaier/readmeGPT";
const readmeWriter = new ReadmeWriter({});
const val = "https://www.val.town/v/:username/:valname";
const successMessage = await readmeWriter.writeReadme(val);
API Reference
Class: ReadmeWriter
The ReadmeWriter
class represents a utility for generating and updating README files.
Constructor
Creates an instance of the ReadmeWriter
class.
Parameters:
model
(optional): The model to be used for generating the readme. Defaults to "gpt-3.5-turbo".apiKey
(optional): An OpenAI API key. Defaults toDeno.env.get("OPENAI_API_KEY")
.
Methods
-
draftReadme(val: string): Promise<string>
: Generates a readme for the given val.-
Parameters:
val
: URL of the code repository.
-
Returns:
- A promise that resolves to the generated readme.
-
-
writeReadme(val: string): Promise<string>
: Generates and updates a readme for the given val.-
Parameters:
val
: URL of the code repository.
-
Returns:
- A promise that resolves to a success message if the update is successful.
-
Todos
- Additional options to pass to the OpenAI model
- Ability to pass more instructions to the prompt to modify how the readme is constructed
SQLite QueryWriter
The QueryWriter class is a utility for generating and executing SQL queries using natural language and OpenAI. It provides a simplified interface for interacting with your Val Town SQLite database and generating SQL queries based on user inputs.
This val is inspired by prisma-gpt. PRs welcome! See Todos below for some ideas I have.
Usage
- Import the QueryWriter class into your script:
- Create an instance of QueryWriter, providing the desired table and an optional model:
- Call the
writeQuery()
method to generate an SQL query based on a user input string:
Create valconst userInput = "Show me all the customers with more than $1000 in purchases.";
const query = await writer.writeQuery(userInput);
- Alternatively, use the
gptQuery()
method to both generate and execute the SQL query:
Create valconst userInput = "Show me all the customers with more than $1000 in purchases.";
const result = await writer.gptQuery(userInput);
- Handle the generated query or query result according to your application's needs.
API
new QueryWriter(args: { table: string; model?: string }): QueryWriter
Creates a new instance of the QueryWriter class.
table
: The name of the database table to operate on.model
(optional): The model to use for generating SQL queries. Defaults to "gpt-3.5-turbo".apiKey
(optional): An OpenAI API key. Defaults toDeno.env.get("OPENAI_API_KEY")
.
writeQuery(str: string): Promise<string>
Generates an SQL query based on the provided user input string.
str
: The user input string describing the desired query.
Returns a Promise that resolves to the generated SQL query.
gptQuery(str: string): Promise<any>
Generates and executes an SQL query based on the provided user input string.
str
: The user input string describing the desired query.
Returns a Promise that resolves to the result of executing the generated SQL query.
Todos
- Handle multiple tables for more complex use cases
- Edit prompt to allow for more than just
SELECT
queries - Allow a user to add to the system prompt maybe?
- Expand usage beyond just Turso SQLite to integrate with other databases