feat: solution for 'Экзамен: Самокорректирующийся агент'
This commit is contained in:
+26
-16
@@ -1,24 +1,34 @@
|
||||
const { OpenAI } = require("langchain-openai");
|
||||
|
||||
// Ensure the OpenAI API key is set in the environment
|
||||
if (!process.env.OPENAI_API_KEY) {
|
||||
console.error("Error: OPENAI_API_KEY environment variable is not set.");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Instantiate the OpenAI LLM with desired parameters
|
||||
const llm = new OpenAI({
|
||||
temperature: 0.7,
|
||||
modelName: "gpt-3.5-turbo",
|
||||
});
|
||||
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() {
|
||||
const prompt = "Hello, world!";
|
||||
// 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 {
|
||||
// Invoke the LLM with the prompt
|
||||
const response = await llm.invoke(prompt);
|
||||
console.log("Response:", response);
|
||||
console.log("LLM response:", response);
|
||||
} catch (error) {
|
||||
console.error("Error invoking LLM:", error);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user