New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
context_length_exceeded | This model's maximum context length is 16385 tokens. However, your messages resulted in 59678 tokens. Please reduce the length of the messages | invalid_request_error #6988
Closed
Labels
Comments
import dotenv from 'dotenv';
dotenv.config();
import { DataSource } from "typeorm";
import { SqlDatabase } from "langchain/sql_db";
import { ChatOpenAI } from "@langchain/openai";
import {
RunnablePassthrough,
RunnableSequence,
} from "@langchain/core/runnables";
import { PromptTemplate } from "@langchain/core/prompts";
import { StringOutputParser } from "@langchain/core/output_parsers";
const datasource = new DataSource({
type: "mysql",
host: process.env.DB_HOST,
port: process.env.DB_PORT,
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
dialect: process.env.DB_DIALECT,
});
const db = await SqlDatabase.fromDataSourceParams({
appDataSource: datasource,
});
const prompt = PromptTemplate.fromTemplate(`Based on the table schema below, write a SQL query that would answer the user's question:
{schema}
Question: {question}
SQL Query:`);
const model = new ChatOpenAI({
openAIApiKey: "sk-XbcwjbY***********lbkFJ3nClZsOpGaSZUgPMK6BY",
});
const sqlQueryGeneratorChain = RunnableSequence.from([
RunnablePassthrough.assign({
schema: async () => db.getTableInfo(),
}),
prompt,
model.bind({ stop: ["\nSQLResult:"] }),
new StringOutputParser(),
]);
const finalResponsePrompt = PromptTemplate.fromTemplate(`Based on the table schema below, question, sql query, and sql response, write a natural language response:
{schema}
Question: {question}
SQL Query: {query}
SQL Response: {response}`);
const fullChain = RunnableSequence.from([
RunnablePassthrough.assign({
query: sqlQueryGeneratorChain,
}),
{
schema: async () => db.getTableInfo(),
question: (input) => input.question,
query: (input) => input.query,
response: (input) => db.run(input.query),
},
finalResponsePrompt,
model,
]);
const finalResponse1 = await fullChain.invoke({
question: ["How many employee present current?"],
});
const finalResponse = await fullChain.invoke({
question: ["can you give names of present employees?"],
});
console.log(finalResponse, finalResponse1);
**It working fine |
This issue has automatically been marked as stale because it has not had any activity in the last 50 days. You can unstale it by commenting or removing the label. Otherwise, this issue will be closed in 10 days. |
This issue was closed automatically because it has been stale for 10 days with no activity. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The text was updated successfully, but these errors were encountered: