-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add stripped down prompt application example
- Loading branch information
1 parent
1fcfc55
commit df62210
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* eslint-disable no-console */ | ||
import { createClient, getPrompt, toSDK } from "../src"; | ||
import OpenAI from "openai"; | ||
|
||
const PROMPT_NAME = process.env.PROMPT_NAME!; | ||
const OPENAI_API_KEY = process.env.OPENAI_API_KEY!; | ||
|
||
// get first argument from command line | ||
const question = process.argv[2]; | ||
|
||
if (!question) { | ||
throw new Error( | ||
"Usage: pnpx tsx examples/apply_prompt.ts 'What is the capital of France?'\nAssumes that the prompt has a variable named 'question'\nAssumes that the prompt is openai with an openai model" | ||
); | ||
} | ||
|
||
if (!OPENAI_API_KEY) { | ||
throw new Error("OPENAI_API_KEY must be provided in the environment"); | ||
} | ||
|
||
const client = createClient({ | ||
options: { | ||
baseUrl: "http://localhost:6006", | ||
}, | ||
}); | ||
|
||
const openai = new OpenAI({ | ||
apiKey: OPENAI_API_KEY, | ||
baseURL: "http://m4-mini:11434/v1", | ||
}); | ||
|
||
const main = async () => { | ||
const prompt = await getPrompt({ | ||
client, | ||
prompt: { name: PROMPT_NAME }, | ||
}); | ||
|
||
const openAIParams = toSDK({ | ||
prompt, | ||
sdk: "openai", | ||
variables: { | ||
question, | ||
}, | ||
}); | ||
|
||
if (!openAIParams) { | ||
throw new Error("Prompt could not be converted to OpenAI params"); | ||
} | ||
|
||
const response = await openai.chat.completions.create({ | ||
...openAIParams, | ||
stream: false, | ||
}); | ||
|
||
console.log(response.choices[0]?.message.content); | ||
}; | ||
|
||
main(); |