37 lines
1.0 KiB
JavaScript
37 lines
1.0 KiB
JavaScript
import { OpenAI } from "langchain-openai";
|
||
import { BaseLLM } from "langchain-core";
|
||
|
||
/**
|
||
* Simple self‑correcting agent demo.
|
||
* Requires an OpenAI API key set in the environment variable OPENAI_API_KEY.
|
||
*/
|
||
async function main() {
|
||
// Ensure the API key is available
|
||
if (!process.env.OPENAI_API_KEY) {
|
||
console.error("Error: OPENAI_API_KEY environment variable is not set.");
|
||
process.exit(1);
|
||
}
|
||
|
||
// Instantiate the OpenAI LLM provider
|
||
const llm = new OpenAI({
|
||
temperature: 0.7,
|
||
// The API key is automatically read from the environment variable
|
||
});
|
||
|
||
// Verify that llm is an instance of BaseLLM (from langchain-core)
|
||
if (!(llm instanceof BaseLLM)) {
|
||
console.error("Error: The LLM instance is not a BaseLLM.");
|
||
process.exit(1);
|
||
}
|
||
|
||
// Send a simple prompt to the LLM
|
||
const prompt = "Hello, world! What is the capital of France?";
|
||
try {
|
||
const response = await llm.invoke(prompt);
|
||
console.log("LLM response:", response);
|
||
} catch (error) {
|
||
console.error("Error invoking LLM:", error);
|
||
}
|
||
}
|
||
|
||
main(); |