Skip to content

Commit

Permalink
chore(api): config sqlite history
Browse files Browse the repository at this point in the history
  • Loading branch information
nicobytes committed Oct 27, 2024
1 parent 74f8105 commit 6cc67ea
Show file tree
Hide file tree
Showing 8 changed files with 273 additions and 8 deletions.
1 change: 1 addition & 0 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"dependencies": {
"@langchain/core": "^0.3.15",
"@langchain/langgraph": "^0.2.18",
"@langchain/langgraph-checkpoint-sqlite": "^0.1.2",
"@langchain/mistralai": "^0.1.1",
"@langchain/openai": "^0.3.11",
"hono": "^4.6.7",
Expand Down
3 changes: 2 additions & 1 deletion apps/api/src/bindings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export type Bindings = {
DB_STORE: D1Database;
JWT_SECRET: string;
CLOUDFLARE_ACCOUNT_ID: string;
CLOUDFLARE_API_TOKEN: string;
Expand All @@ -11,4 +10,6 @@ export type Bindings = {
R2_URL: string;
FB_TOKEN: string;
FB_VERIFY_TOKEN: string;
HISTORY_KV: KVNamespace;
HISTORY_DB: D1Database;
};
1 change: 1 addition & 0 deletions apps/api/src/chatbot/graph.state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { MyNodes } from "./nodes";
export const GraphState = Annotation.Root({
...MessagesAnnotation.spec,
lastAgent: Annotation<MyNodes>,
isReadyToBook: Annotation<boolean>,
});

export type GraphState = typeof GraphState.State;
6 changes: 3 additions & 3 deletions apps/api/src/chatbot/graph.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { END, START, StateGraph } from "@langchain/langgraph";
import { MemorySaver } from "@langchain/langgraph";
import { SqliteSaver } from "@langchain/langgraph-checkpoint-sqlite";

import { GraphState } from "./graph.state";

Expand All @@ -20,8 +20,8 @@ interface Props {
mistralKey: string;
}

export const createGraph = (data: Props) => {
const memory = new MemorySaver();
export const createGraph = (data: Props, db: D1Database) => {
const memory = new SqliteSaver(db);

const llmGpt4 = models.gpt4(data.openAIKey);
const llmMistral = models.mistral(data.mistralKey);
Expand Down
3 changes: 2 additions & 1 deletion apps/api/src/chatbot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ app.post("/webhook", async (c) => {
const fbToken = c.env.FB_TOKEN;
const openAIKey = c.env.OPENAI_API_KEY;
const mistralKey = c.env.MISTRAL_API_KEY;
const d1 = c.env.HISTORY_DB;

const body = await c.req.json();
const message = body.entry?.[0]?.changes[0]?.value?.messages?.[0];
Expand All @@ -38,7 +39,7 @@ app.post("/webhook", async (c) => {
headers.append("Authorization", `Bearer ${fbToken}`);
headers.append("Content-Type", "application/json");

const agent = createGraph({ openAIKey, mistralKey });
const agent = createGraph({ openAIKey, mistralKey }, d1);
const userMessage = message.text.body;
const threadId = `thread_${message.from}`;
const agentResponse = await agent.invoke(
Expand Down
1 change: 1 addition & 0 deletions apps/api/src/chatbot/nodes/booking.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const bookingNode = (llm: ChatOpenAI) => {
return {
messages: response,
lastAgent: MyNodes.BOOKING,
isReadyToBook: true,
};
};
};
11 changes: 10 additions & 1 deletion apps/api/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,13 @@ binding = "AI"

[observability]
enabled = true
head_sampling_rate = 1
head_sampling_rate = 1

[[kv_namespaces]]
binding = "HISTORY_KV"
id = "efd83beca3174ee2ba9bdeaf99f73b4d"

[[d1_databases]]
binding = "HISTORY_DB"
database_name = "prod-d1-history"
database_id = "482cf989-efd8-45ae-a3cd-5e5891a0e573"
Loading

0 comments on commit 6cc67ea

Please sign in to comment.