import XMLHttpRequestPolyfill from "xhr4sw";
/**
* This is needed to polyfill as otherwise we get HttpRequest not defined
*/
Object.defineProperty(self, 'XMLHttpRequest', {
configurable: false,
enumerable: true,
writable: false,
value: XMLHttpRequestPolyfill
});
export const memoryApp = new Hono<{
Bindings: {
OPENAI_API_KEY: string;
MOMENT_API_KEY: string;
}
}>()
const cacheClient = new CacheClient({
configuration: Configurations.Laptop.v1(),
credentialProvider: CredentialProvider.fromString({
apiKey: c.env.MOMENT_API_KEY,
}),
defaultTtlSeconds: 60,
});
// Create a unique session ID
const sessionId = new Date().toISOString();
const cacheName = "langchain";
const memory = new BufferMemory({
chatHistory: await MomentoChatMessageHistory.fromProps({
client: cacheClient,
cacheName,
sessionId,
sessionTtl: 300,
}),
});
console.log(
`cacheName=${cacheName} and sessionId=${sessionId} . This will be used to store the chat history. You can inspect the values at your Momento console at https://console.gomomento.com.`
);
その後は会話を行うためのChainを、OpenAIなどのモデルを使って用意します。
const model = new ChatOpenAI({
modelName: "gpt-3.5-turbo",
temperature: 0,
openAIApiKey: c.env.OPENAI_API_KEY
});
const chain = new ConversationChain({ llm: model, memory });
const res1 = await chain.call({ input: "Hi! I'm Jim." });
console.log({ res1 });
const res2 = await chain.call({ input: "What did I just say my name was?" });
console.log({ res2 });
return c.json({
res1,
res2
})
import { Hono } from "hono";
import {
CacheClient,
Configurations,
CredentialProvider,
} from "@gomomento/sdk-web"; // `from "gomomento/sdk-web";` for browser/edge
import { BufferMemory } from "langchain/memory";
import { ChatOpenAI } from "langchain/chat_models/openai";
import { ConversationChain } from "langchain/chains";
import { MomentoChatMessageHistory } from "langchain/stores/message/momento";
import XMLHttpRequestPolyfill from "xhr4sw";
/**
* This is needed to polyfill as otherwise we get HttpRequest not defined
*/
Object.defineProperty(self, 'XMLHttpRequest', {
configurable: false,
enumerable: true,
writable: false,
value: XMLHttpRequestPolyfill
});
export const memoryApp = new Hono<{
Bindings: {
OPENAI_API_KEY: string;
MOMENT_API_KEY: string;
}
}>()
memoryApp.get('test', async c => {
const cacheClient = new CacheClient({
configuration: Configurations.Laptop.v1(),
credentialProvider: CredentialProvider.fromString({
apiKey: c.env.MOMENT_API_KEY,
}),
defaultTtlSeconds: 60,
});
// Create a unique session ID
const sessionId = new Date().toISOString();
const cacheName = "langchain";
const memory = new BufferMemory({
chatHistory: await MomentoChatMessageHistory.fromProps({
client: cacheClient,
cacheName,
sessionId,
sessionTtl: 300,
}),
});
console.log(
`cacheName=${cacheName} and sessionId=${sessionId} . This will be used to store the chat history. You can inspect the values at your Momento console at https://console.gomomento.com.`
);
const model = new ChatOpenAI({
modelName: "gpt-3.5-turbo",
temperature: 0,
openAIApiKey: c.env.OPENAI_API_KEY
});
const chain = new ConversationChain({ llm: model, memory });
const res1 = await chain.call({ input: "Hi! I'm Jim." });
console.log({ res1 });
const res2 = await chain.call({ input: "What did I just say my name was?" });
console.log({ res2 });
return c.json({
res1,
res2
})
})
% curl http://127.0.0.1:8787/memory/test | jq .
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 144 100 144 0 0 35 0 0:00:04 0:00:04 --:--:-- 35
{
"res1": {
"response": "Hello Jim! It's nice to meet you. How can I assist you today?"
},
"res2": {
"response": "You just said that your name is Jim."
}
}