diff --git a/examples/online-store-customer-service/README.md b/examples/online-store-customer-service/README.md new file mode 100755 index 00000000..10415245 --- /dev/null +++ b/examples/online-store-customer-service/README.md @@ -0,0 +1,109 @@ +![Demo Online Store (TechBay) Customer Service Chatbot by ⌘ Langbase][cover] + +![License: MIT][mit] [![Fork to ⌘ Langbase][fork]][pipe-main-chatbot] + +## Build an Online Store (TechBay) Customer Service Chatbot with Pipes β€” ⌘ Langbase + +This chatbot is built by using an multiple AI Pipes (Chatbot agents) on Langbase, it works with 30+ LLMs (OpenAI, Gemini, Mistral, Llama, Gemma, etc), any Data (10M+ context with Memory sets), and any Framework (standard web API you can use with any software). + + +Check out the live demo [here][demo]. + +## Features + +- πŸ’¬ [Online Store (TechBay) Customer Service Chatbot][demo] β€” Built with an [AI Pipe on ⌘ Langbase][pipe-main-chatbot] +- ⚑️ Streaming β€” Real-time chat experience with streamed responses +- πŸ—£οΈ Q/A β€” Ask questions and get pre-defined answers with your preferred AI model and tone +- πŸ”‹ Responsive and open source β€” Works on all devices and platforms + + + +## Learn more + +1. Take a look at the main chatbot pipe that routes call for other departments in the online store [Online Store (TechBay) Customer Service Main Chatbot Pipe on ⌘ Langbase][pipe-main-chatbot] +2. Take a look at the electronics section pipe of the demo online store chatbot that classifies routed customer query into three categories i.e, 1. Support, Feedback, Complaint. 2. Order Tracking. 3. Refund/Exchange. [Online Store (TechBay) Customer Service Electronics Department Agent Pipe on ⌘ Langbase][pipe-electronics-dept] +3. Take a look at the sports gear section pipe of the demo online store chatbot that classifies routed customer query into three categories i.e, 1. Support, Feedback, Complaint. 2. Order Tracking. 3. Refund/Exchange. [Online Store (TechBay) Customer Service Sports Gear Department Agent Pipe on ⌘ Langbase][pipe-electronics-dept] +4. Take a look at the travel bags section pipe of the demo online store chatbot that classifies routed customer query into three categories i.e, 1. Support, Feedback, Complaint. 2. Order Tracking. 3. Refund/Exchange. [Online Store (TechBay) Customer Service Travel Bags Department Agent Pipe on ⌘ Langbase][pipe-electronics-dept] +2. Read the [source code on GitHub][gh] for this example +3. Go through Documentaion: [Pipe Quick Start][qs] +4. Learn more about [Pipes & Memory features on ⌘ Langbase][docs] + + +## Get started + +Let's get started with the project: + +To get started with Langbase, you'll need to [create a free personal account on Langbase.com][signup] and verify your email address. _Done? Cool, cool!_ + +1. Fork the [Online Store (TechBay) Customer Service Main Chatbot Pipe][pipe-main-chatbot] Pipe on ⌘ Langbase. +2. Fork the [Online Store (TechBay) Customer Service Sports Gear Department Agent Pipe][pipe-sports-gear] Pipe on ⌘ Langbase. +3. Fork the [Online Store (TechBay) Customer Service Electronics Department Agent Pipe][pipe-electronics-dept] Pipe on ⌘ Langbase. +4. Fork the [Online Store (TechBay) Customer Service Travel Bags Department Agent Pipe][pipe-travel-bags-dept] Pipe on ⌘ Langbase. +5. Go to the API tab to copy the Pipe's API key (to be used on server-side only). +6. Download the example project folder from [here][download] or clone the reppository. +7. `cd` into the online-cs-chatbot-backend directory and open it in your code editor. +8. Duplicate the `.dev.var.example` file in this project and rename it to `.dev.vars`. +9. Add the following environment variables (.dev.vars): +``` + # Replace `PIPE_API_KEY` with the copied API key. + LANGBASE_ONLINE_STORE_CUSTOMER_SERVICE_API_KEY="PIPE_API_KEY" + LANGBASE_TRAVEL_PIPE_API_KEY="PIPE_API_KEY" + LANGBASE_ELECTRONICS_PIPE_API_KEY="PIPE_API_KEY" + LANGBASE_SPORTS_PIPE_API_KEY="PIPE_API_KEY" +``` + +10. Issue the following in your CLI separately for online-cs-chatbot directory (frontend of the chatbot) and online-cs-chatbot-backend directory (backend of the chatbot) : +```sh + # Install the dependencies using the following command: + npm install + + # Run the project using the following command: + npm run dev +``` + +11. Your app template (frontend of the chatbot) should now be running on [localhost:3000][localfrontend]. + +> NOTE: +> This is a Next.js project, so you can build and deploy it to any platform of your choice, like Vercel, Netlify, Cloudflare, etc. + +12. Your backend which is a cloudflare worker should now be running on [localhost:8787][localbackend]. + +> NOTE: +> This project requires cloudflare account so that cloudflare CLI [Cloudflare CLI account and setup][cloudflare] + +13. To test the classification and function call you can the following test dataset: +``` +Electronic Dept. Query: + I order 2 days ago a new Sony Bravia XR-65X90L, I did recieve confirmation that my order will be shipped shortly but I have not recieve any tracking number or any confirmation that the order has been disptachen. Please reply soon as I order because you guys ship fast and it should not take more then a week. + +Travel Bags Dept. Query: + I have received Wenger backpack for 15 inch laptop two days ago, however it quite small for my use case. I want return and order new Wenger 30L for 16inch laptop. Let me know if it is in stock? + +Sports Gear Dept. Query: + I bought Nike zoomx two days ago, although I received the order confirmation and the receipt but I have not received the tracking number. Please send the tracking number asap. +``` + +## Authors + +This project is created by [Langbase][lb] team members, with contributions from: + +- Muhammad-Ali Danish - Software Engineer, [Langbase][lb]
+**_Built by ⌘ [Langbase.com][lb] β€” Ship hyper-personalized AI assistants with memory!_** + +[cover]:https://raw.githubusercontent.com/LangbaseInc/docs-images/main/examples/online-store-customer-service/online-store-customer-service.png +[lb]: https://langbase.com +[demo]: https://online-store-customer-service.langbase.dev +[pipe-main-chatbot]: https://beta.langbase.com/examples/online-store-customer-service +[pipe-electronics-dept]: https://beta.langbase.com/examples/online-store-electronics-dept +[pipe-travel-bags-dept]: https://beta.langbase.com/examples/online-store-travel-bags-dept +[pipe-sports-gear]: https://beta.langbase.com/examples/online-store-sports-gear-dept +[gh]: https://github.com/LangbaseInc/langbase-examples/tree/main/examples/online-store-customer-service +[download]:https://download-directory.github.io/?url=https://github.com/LangbaseInc/langbase-examples/tree/main/examples/online-store-customer-service +[qs]:https://langbase.com/docs/pipe/quickstart +[docs]:https://langbase.com/docs +[local]:http://localhost:3000 +[mit]: https://img.shields.io/badge/license-MIT-blue.svg?style=for-the-badge&color=%23000000 +[fork]: https://img.shields.io/badge/FORK%20ON-%E2%8C%98%20Langbase-000000.svg?style=for-the-badge&logo=%E2%8C%98%20Langbase&logoColor=000000 +[localfrontend]:http://localhost:3000 +[localbackend]:http://localhost:8787 +[cloudflare]:https://developers.cloudflare.com/workers/get-started/guide/ \ No newline at end of file diff --git a/examples/online-store-customer-service/online-cs-chatbot-backend/.dev.vars.example b/examples/online-store-customer-service/online-cs-chatbot-backend/.dev.vars.example new file mode 100644 index 00000000..60a0c0b1 --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot-backend/.dev.vars.example @@ -0,0 +1,4 @@ +LANGBASE_ONLINE_STORE_CUSTOMER_SERVICE_API_KEY="" +LANGBASE_TRAVEL_PIPE_API_KEY="" +LANGBASE_ELECTRONICS_PIPE_API_KEY="" +LANGBASE_SPORTS_PIPE_API_KEY="" \ No newline at end of file diff --git a/examples/online-store-customer-service/online-cs-chatbot-backend/.gitignore b/examples/online-store-customer-service/online-cs-chatbot-backend/.gitignore new file mode 100644 index 00000000..e0773208 --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot-backend/.gitignore @@ -0,0 +1,183 @@ +# Logs + +logs +_.log +npm-debug.log_ +yarn-debug.log* +yarn-error.log* +lerna-debug.log* +.pnpm-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) + +report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json + +# Runtime data + +pids +_.pid +_.seed +\*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover + +lib-cov + +# Coverage directory used by tools like istanbul + +coverage +\*.lcov + +# nyc test coverage + +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) + +.grunt + +# Bower dependency directory (https://bower.io/) + +bower_components + +# node-waf configuration + +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) + +build/Release + +# Dependency directories + +node_modules/ +jspm_packages/ + +# Snowpack dependency directory (https://snowpack.dev/) + +web_modules/ + +# TypeScript cache + +\*.tsbuildinfo + +# Optional npm cache directory + +.npm + +# Optional eslint cache + +.eslintcache + +# Optional stylelint cache + +.stylelintcache + +# Microbundle cache + +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history + +.node_repl_history + +# Output of 'npm pack' + +\*.tgz + +# Yarn Integrity file + +.yarn-integrity + +# dotenv environment variable files + +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# parcel-bundler cache (https://parceljs.org/) + +.cache +.parcel-cache + +# Next.js build output + +.next +out + +# Nuxt.js build / generate output + +.nuxt +dist + +# Gatsby files + +.cache/ + +# Comment in the public line in if your project uses Gatsby and not Next.js + +# https://nextjs.org/blog/next-9-1#public-directory-support + +# public + +# vuepress build output + +.vuepress/dist + +# vuepress v2.x temp and cache directory + +.temp +.cache + +# Docusaurus cache and generated files + +.docusaurus + +# Serverless directories + +.serverless/ + +# FuseBox cache + +.fusebox/ + +# DynamoDB Local files + +.dynamodb/ + +# TernJS port file + +.tern-port + +# Stores VSCode versions used for testing VSCode extensions + +.vscode-test + +# yarn v2 + +.yarn/cache +.yarn/unplugged +.yarn/build-state.yml +.yarn/install-state.gz +.pnp.\* + +# wrangler project + +.dev.vars +.wrangler/ + +.vscode + + +# No lock files. +package-lock.json +yarn.lock + + + +.editorconfig diff --git a/examples/online-store-customer-service/online-cs-chatbot-backend/.prettierrc b/examples/online-store-customer-service/online-cs-chatbot-backend/.prettierrc new file mode 100644 index 00000000..5c7b5d3c --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot-backend/.prettierrc @@ -0,0 +1,6 @@ +{ + "printWidth": 140, + "singleQuote": true, + "semi": true, + "useTabs": true +} diff --git a/examples/online-store-customer-service/online-cs-chatbot-backend/package.json b/examples/online-store-customer-service/online-cs-chatbot-backend/package.json new file mode 100644 index 00000000..5673f52f --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot-backend/package.json @@ -0,0 +1,26 @@ +{ + "name": "summer-flower-b680", + "version": "0.0.0", + "private": true, + "scripts": { + "deploy": "wrangler deploy", + "dev": "wrangler dev", + "start": "wrangler dev", + "test": "vitest", + "build": "wrangler build", + "cf-typegen": "wrangler types" + }, + "devDependencies": { + "@cloudflare/vitest-pool-workers": "^0.4.5", + "@cloudflare/workers-types": "^4.20240620.0", + "typescript": "^5.4.5", + "vitest": "1.5.0", + "wrangler": "^3.60.3" + }, + "dependencies": { + "ai": "^3.0.16", + "cheerio": "^1.0.0-rc.12", + "langbase": "^0.2.5", + "openai": "^4.52.0" + } +} diff --git a/examples/online-store-customer-service/online-cs-chatbot-backend/src/index.ts b/examples/online-store-customer-service/online-cs-chatbot-backend/src/index.ts new file mode 100644 index 00000000..10f5b2fd --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot-backend/src/index.ts @@ -0,0 +1,323 @@ +import { Env, ApiResponse } from './types'; + +export interface InternalMessage { + entity: 'main' | 'internal'; + responseStream: ReadableStream; + toolCallDetected: boolean; + customerQuery?: string; + threaId: string; +} + +const encoder = new TextEncoder(); + +function getDepartmentKey(functionName: string): keyof Pick | null { + const departmentMap: { [key: string]: keyof Pick } = { + 'call_sports_dept': 'LANGBASE_SPORTS_PIPE_API_KEY', + 'call_electronics_dept': 'LANGBASE_ELECTRONICS_PIPE_API_KEY', + 'call_travel_dept': 'LANGBASE_TRAVEL_PIPE_API_KEY' + }; + return departmentMap[functionName] || null; +} + +async function callDepartment(deptKey: keyof Pick, customerQuery: string, env: Env, threadId?: string): Promise { + + const response = await fetch('https://api.langbase.com/beta/chat', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${env[deptKey]}`, + }, + body: JSON.stringify({ + messages: [{ role: 'user', content: customerQuery }], + ...(threadId && { threadId }) + }), + }); + + if (!response.ok) { + throw new Error(`Error: ${response.status} ${response.statusText}`); + } + + return await response.json() as ApiResponse; +} + + + async function processMainChatbotResponse(response: Response, env: Env, threadId: string, initialState: { entity: 'main' | 'internal', toolCallDetected: boolean }): Promise { + const reader = response.body!.getReader(); + const decoder = new TextDecoder(); + + let accumulatedToolCall: any = null; + let accumulatedArguments = ''; + const sharedState = { + toolCallDetected: initialState.toolCallDetected, + entity: initialState.entity + }; + + const [processStream, responseStream] = new ReadableStream({ + async start(controller: ReadableStreamDefaultController) { + try { + let buffer = ''; + while (true) { + const { done, value } = await reader.read(); + if (done) break; + + buffer += decoder.decode(value, { stream: true }); + const lines = buffer.split('\n'); + buffer = lines.pop() || ''; + + for (const line of lines) { + const result = await processLine(line, controller, env, threadId, accumulatedToolCall, accumulatedArguments, sharedState); + accumulatedToolCall = result[0]; + accumulatedArguments = result[1]; + } + } + } catch (error) { + console.error('Error in stream processing:', error); + } finally { + controller.close(); + + } + } + }).tee(); + + await processStream.pipeTo(new WritableStream()); + + return { + entity: sharedState.toolCallDetected ? 'internal' : 'main', + responseStream: responseStream, + toolCallDetected: sharedState.toolCallDetected, + customerQuery: '', + threadId: threadId + }; +} + +async function processLine( + line: string, + controller: ReadableStreamDefaultController, + env: Env, + threadId: string, + accumulatedToolCall: any, + accumulatedArguments: string, + sharedState: { toolCallDetected: boolean } +): Promise<[any, string]> { + if (line.trim() === 'data: [DONE]') { + controller.enqueue(encoder.encode('data: [DONE]\n\n')); + return [accumulatedToolCall, accumulatedArguments]; + } + if (!line.startsWith('data: ')) return [accumulatedToolCall, accumulatedArguments]; + + try { + const data = JSON.parse(line.slice(6)); + if (!data.choices || !data.choices[0].delta) return [accumulatedToolCall, accumulatedArguments]; + + const delta = data.choices[0].delta; + if (delta.content) { + enqueueContentChunk(controller, data, delta.content); + } else if (delta.tool_calls) { + sharedState.toolCallDetected = true; + if (!accumulatedToolCall) { + accumulatedToolCall = delta.tool_calls[0]; + accumulatedArguments = delta.tool_calls[0].function.arguments || ''; + } else { + accumulatedArguments += delta.tool_calls[0].function.arguments || ''; + } + + if (accumulatedToolCall.function.name && accumulatedArguments.endsWith('}')) { + const departmentKey = getDepartmentKey(accumulatedToolCall.function.name); + if (departmentKey) { + const args = JSON.parse(accumulatedArguments); + const departmentResponse = await callDepartment(departmentKey, args.customerQuery, env, threadId); + let responseContent = formatDepartmentResponse(departmentResponse.completion); + console.log(`Dept response: ${responseContent}`); + enqueueContentChunk(controller, data, responseContent); + } + accumulatedToolCall = null; + accumulatedArguments = ''; + } + } + } catch (error) { + console.warn('Error processing chunk:', error, 'Line:', line); + } + + return [accumulatedToolCall, accumulatedArguments]; +} + +function enqueueContentChunk(controller: ReadableStreamDefaultController, data: any, content: string) { + const chunk = { + id: data.id, + object: data.object, + created: data.created, + model: data.model, + choices: [{ index: 0, delta: { content }, finish_reason: null }] + }; + controller.enqueue(encoder.encode(`data: ${JSON.stringify(chunk)}\n\n`)); +} + +function formatDepartmentResponse(response: string): string { + try { + const parsedResponse = JSON.parse(response); + const [key, value] = Object.entries(parsedResponse)[0]; + return `${key} ${value}`; + } catch (parseError) { + console.warn('Error parsing department response:', parseError); + return response; + } +} + +async function callMainChatbot(query: string, threadId: string | undefined, env: Env): Promise { + const userQuery = { + threadId, + messages: [{ role: 'user', content: query }], + }; + return fetch('https://api.langbase.com/beta/chat', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${env['LANGBASE_ONLINE_STORE_CUSTOMER_SERVICE_API_KEY']}`, + }, + body: JSON.stringify(userQuery), + }); +} + +export default { + async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise { + const isProduction = request.url.includes('workers.dev'); + const allowedOrigin = isProduction ? 'https://online-cs.pages.dev' : 'http://localhost:3000'; + + const MAX_USERS = 2; // MAX_USERS also represents different conversation thread for the same user (reset case) + const MAX_CONVERSATIONS = 3; + const EXPIRATION_TTL = 300; // 5 min test + + if (request.method === 'OPTIONS') { + return new Response(null, { + headers: { + 'Access-Control-Allow-Origin': allowedOrigin, + 'Access-Control-Allow-Methods': 'POST, OPTIONS', + 'Access-Control-Allow-Headers': 'Content-Type', + }, + }); + } + + if (request.method !== 'POST') { + return new Response('Method Not Allowed', { + status: 405, + headers: { + 'Access-Control-Allow-Origin': allowedOrigin, + } + }); + } + + let threadId = request.headers.get('lb-thread-id') || undefined; + + // Rate limiting + const activeUsersJson = await env.CACHE.get('active_users'); + const activeUsers = activeUsersJson ? JSON.parse(activeUsersJson) : {}; + const currentTime = Date.now(); + + if (threadId && activeUsers[threadId]) { + if (activeUsers[threadId].count >= MAX_CONVERSATIONS) { + return new Response('You have reached the maximum number of conversations', { status: 429 }); + } + } else if (Object.keys(activeUsers).length >= MAX_USERS) { + return new Response('Maximum number of concurrent users reached', { status: 429 }); + } + + let incomingMessages; + try { + const body = await request.json() as { messages: any[], threadId?: string }; + incomingMessages = body.messages; + threadId = body.threadId || threadId; + + if (!incomingMessages || !Array.isArray(incomingMessages) || incomingMessages.length === 0) { + throw new Error('Invalid or empty messages array'); + } + } catch (error) { + console.error('Error processing request:', (error as Error).message); + return new Response(JSON.stringify({ error: (error as Error).message }), { + status: 400, + headers: { + 'Content-Type': 'application/json', + 'Access-Control-Allow-Origin': allowedOrigin, + }, + }); + } + + let internalMessage: InternalMessage; + let query = incomingMessages[incomingMessages.length - 1].content; + + do { + const response = await callMainChatbot(query, threadId, env); + threadId = response.headers.get('lb-thread-id') || threadId; + + if (!response.body) { + console.error('No readable stream found in response body'); + return new Response('Internal Server Error', { status: 500 }); + } + + internalMessage = await processMainChatbotResponse(response, env, threadId || '', { entity: 'main', toolCallDetected: false }); + if (internalMessage.entity === 'internal') { + const response = await callMainChatbot('summarize the current status for the customer', threadId, env); + threadId = response.headers.get('lb-thread-id') || threadId; + + if (!response.body) { + console.error('No readable stream found in response body'); + return new Response('Internal Server Error', { status: 500 }); + } + + internalMessage = await processMainChatbotResponse(response, env, threadId || '', { entity: 'main', toolCallDetected: false }); + } + } while (internalMessage.entity === 'internal'); + + + // Update rate limiting data + if (threadId) { + if (!activeUsers[threadId]) { + activeUsers[threadId] = { count: 0, lastAccess: currentTime }; + } + activeUsers[threadId].count++; + activeUsers[threadId].lastAccess = currentTime; + + await env.CACHE.put('active_users', JSON.stringify(activeUsers), { expirationTtl: EXPIRATION_TTL }); + + // Log KV contents + const value = await env.CACHE.list(); + console.log(`CACHE KVs: ${JSON.stringify(value.keys)}`); + for (const key of value.keys) { + const keyValue = await env.CACHE.get(key.name); + console.log(`Key: ${key.name}, Value: ${keyValue}`); + } + + // Cleanup expired users + ctx.waitUntil(cleanupExpiredUsers(env, MAX_USERS, MAX_CONVERSATIONS, EXPIRATION_TTL)); + } + + return new Response(internalMessage.responseStream, { + headers: { + 'Content-Type': 'text/event-stream', + 'Cache-Control': 'no-cache', + 'Connection': 'keep-alive', + 'Access-Control-Allow-Origin': allowedOrigin, + 'Access-Control-Allow-Methods': 'POST, OPTIONS', + 'Access-Control-Allow-Headers': 'Content-Type, lb-thread-id', + 'lb-thread-id': threadId || '', + }, + }); + }, +}; + + +async function cleanupExpiredUsers(env: Env, MAX_USERS: number, MAX_CONVERSATIONS: number, EXPIRATION_TTL: number) { + const activeUsersJson = await env.CACHE.get('active_users'); + if (!activeUsersJson) return; + + const activeUsers = JSON.parse(activeUsersJson); + const currentTime = Date.now(); + const oneHourAgo = currentTime - EXPIRATION_TTL * 1000; + + for (const [threadId, userData] of Object.entries(activeUsers)) { + if (userData.lastAccess < oneHourAgo) { + delete activeUsers[threadId]; + } + } + + await env.CACHE.put('active_users', JSON.stringify(activeUsers), { expirationTtl: EXPIRATION_TTL }); +} \ No newline at end of file diff --git a/examples/online-store-customer-service/online-cs-chatbot-backend/src/types.ts b/examples/online-store-customer-service/online-cs-chatbot-backend/src/types.ts new file mode 100644 index 00000000..8cdaf8d4 --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot-backend/src/types.ts @@ -0,0 +1,70 @@ +interface LangbaseResponse { + completion: string; +} + +interface ToolCall { + id: string; + type: string; + function: { + name: string; + arguments: string; + }; +} + +interface AssistantMessage { + role: string; + content: string | null; + tool_calls?: ToolCall[]; +} + +interface Choice { + index: number; + message: AssistantMessage; + logprobs: null; + finish_reason: string; +} + +interface Usage { + prompt_tokens: number; + completion_tokens: number; + total_tokens: number; +} + +interface RawResponse { + id: string; + object: string; + created: number; + model: string; + choices: Choice[]; + usage: Usage; + system_fingerprint: null; +} + +interface ApiResponse { + success: boolean; + completion: string; + raw: RawResponse; +} + +interface Env { + OPENAI_API_KEY: string; + LANGBASE_TRAVEL_PIPE_API_KEY: string, + LANGBASE_ELECTRONICS_PIPE_API_KEY: string, + LANGBASE_SPORTS_PIPE_API_KEY: string, + LANGBASE_ONLINE_STORE_CUSTOMER_SERVICE_API_KEY: string, + CACHE: KVNamespace +} + +export type { + LangbaseResponse, + ToolCall, + AssistantMessage, + Choice, + Usage, + RawResponse, + ApiResponse, + Env +}; + + + diff --git a/examples/online-store-customer-service/online-cs-chatbot-backend/test/index.spec.ts b/examples/online-store-customer-service/online-cs-chatbot-backend/test/index.spec.ts new file mode 100644 index 00000000..fbee335d --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot-backend/test/index.spec.ts @@ -0,0 +1,25 @@ +// test/index.spec.ts +import { env, createExecutionContext, waitOnExecutionContext, SELF } from 'cloudflare:test'; +import { describe, it, expect } from 'vitest'; +import worker from '../src/index'; + +// For now, you'll need to do something like this to get a correctly-typed +// `Request` to pass to `worker.fetch()`. +const IncomingRequest = Request; + +describe('Hello World worker', () => { + it('responds with Hello World! (unit style)', async () => { + const request = new IncomingRequest('http://example.com'); + // Create an empty context to pass to `worker.fetch()`. + const ctx = createExecutionContext(); + const response = await worker.fetch(request, env, ctx); + // Wait for all `Promise`s passed to `ctx.waitUntil()` to settle before running test assertions + await waitOnExecutionContext(ctx); + expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`); + }); + + it('responds with Hello World! (integration style)', async () => { + const response = await SELF.fetch('https://example.com'); + expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`); + }); +}); diff --git a/examples/online-store-customer-service/online-cs-chatbot-backend/test/tsconfig.json b/examples/online-store-customer-service/online-cs-chatbot-backend/test/tsconfig.json new file mode 100644 index 00000000..bc019a7e --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot-backend/test/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "types": ["@cloudflare/workers-types/experimental", "@cloudflare/vitest-pool-workers"] + }, + "include": ["./**/*.ts", "../src/env.d.ts"], + "exclude": [] +} diff --git a/examples/online-store-customer-service/online-cs-chatbot-backend/tsconfig.json b/examples/online-store-customer-service/online-cs-chatbot-backend/tsconfig.json new file mode 100644 index 00000000..132ca912 --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot-backend/tsconfig.json @@ -0,0 +1,104 @@ +{ + "compilerOptions": { + /* Visit https://aka.ms/tsconfig.json to read more about this file */ + + /* Projects */ + // "incremental": true, /* Enable incremental compilation */ + // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */ + // "tsBuildInfoFile": "./", /* Specify the folder for .tsbuildinfo incremental compilation files. */ + // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects */ + // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */ + // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ + + /* Language and Environment */ + "target": "es2021" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */, + "lib": ["es2021"] /* Specify a set of bundled library declaration files that describe the target runtime environment. */, + "jsx": "react" /* Specify what JSX code is generated. */, + // "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */ + // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */ + // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h' */ + // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */ + // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using `jsx: react-jsx*`.` */ + // "reactNamespace": "", /* Specify the object invoked for `createElement`. This only applies when targeting `react` JSX emit. */ + // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */ + // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ + + /* Modules */ + "module": "es2022" /* Specify what module code is generated. */, + // "rootDir": "./", /* Specify the root folder within your source files. */ + "moduleResolution": "Bundler" /* Specify how TypeScript looks up a file from a given module specifier. */, + // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ + // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ + // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ + // "typeRoots": [], /* Specify multiple folders that act like `./node_modules/@types`. */ + "types": [ + "@cloudflare/workers-types/2023-07-01" + ] /* Specify type package names to be included without being referenced in a source file. */, + // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ + "resolveJsonModule": true /* Enable importing .json files */, + // "noResolve": true, /* Disallow `import`s, `require`s or ``s from expanding the number of files TypeScript should add to a project. */ + + /* JavaScript Support */ + "allowJs": true /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */, + "checkJs": true /* Enable error reporting in type-checked JavaScript files. */, + // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */ + + /* Emit */ + // "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ + // "declarationMap": true, /* Create sourcemaps for d.ts files. */ + // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ + // "sourceMap": true, /* Create source map files for emitted JavaScript files. */ + // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */ + // "outDir": "./", /* Specify an output folder for all emitted files. */ + // "removeComments": true, /* Disable emitting comments. */ + "noEmit": true /* Disable emitting files from a compilation. */, + // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ + // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */ + // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */ + // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */ + // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ + // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ + // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */ + // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */ + // "newLine": "crlf", /* Set the newline character for emitting files. */ + // "stripInternal": true, /* Disable emitting declarations that have `@internal` in their JSDoc comments. */ + // "noEmitHelpers": true, /* Disable generating custom helper functions like `__extends` in compiled output. */ + // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ + // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */ + // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ + // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ + + /* Interop Constraints */ + "isolatedModules": true /* Ensure that each file can be safely transpiled without relying on other imports. */, + "allowSyntheticDefaultImports": true /* Allow 'import x from y' when a module doesn't have a default export. */, + // "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */, + // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ + "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */, + + /* Type Checking */ + "strict": true /* Enable all strict type-checking options. */, + // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */ + // "strictNullChecks": true, /* When type checking, take into account `null` and `undefined`. */ + // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ + // "strictBindCallApply": true, /* Check that the arguments for `bind`, `call`, and `apply` methods match the original function. */ + // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */ + // "noImplicitThis": true, /* Enable error reporting when `this` is given the type `any`. */ + // "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */ + // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */ + // "noUnusedLocals": true, /* Enable error reporting when a local variables aren't read. */ + // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read */ + // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */ + // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */ + // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */ + // "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */ + // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */ + // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type */ + // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */ + // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */ + + /* Completeness */ + // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ + "skipLibCheck": true /* Skip type checking all .d.ts files. */ + }, + "exclude": ["test"] +} diff --git a/examples/online-store-customer-service/online-cs-chatbot-backend/vitest.config.mts b/examples/online-store-customer-service/online-cs-chatbot-backend/vitest.config.mts new file mode 100644 index 00000000..503a7174 --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot-backend/vitest.config.mts @@ -0,0 +1,11 @@ +import { defineWorkersConfig } from '@cloudflare/vitest-pool-workers/config'; + +export default defineWorkersConfig({ + test: { + poolOptions: { + workers: { + wrangler: { configPath: './wrangler.toml' }, + }, + }, + }, +}); diff --git a/examples/online-store-customer-service/online-cs-chatbot-backend/worker-configuration.d.ts b/examples/online-store-customer-service/online-cs-chatbot-backend/worker-configuration.d.ts new file mode 100644 index 00000000..5b2319b3 --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot-backend/worker-configuration.d.ts @@ -0,0 +1,4 @@ +// Generated by Wrangler +// After adding bindings to `wrangler.toml`, regenerate this interface via `npm run cf-typegen` +interface Env { +} diff --git a/examples/online-store-customer-service/online-cs-chatbot-backend/wrangler.toml b/examples/online-store-customer-service/online-cs-chatbot-backend/wrangler.toml new file mode 100644 index 00000000..24209f66 --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot-backend/wrangler.toml @@ -0,0 +1,112 @@ +#:schema node_modules/wrangler/config-schema.json +name = "summer-flower-b680" +main = "src/index.ts" +compatibility_date = "2024-06-20" +compatibility_flags = ["nodejs_compat"] + +[[kv_namespaces]] +binding = "CACHE" +id = "28a0850619674663a63eee37e90ab7a3" + +# Automatically place your workloads in an optimal location to minimize latency. +# If you are running back-end logic in a Worker, running it closer to your back-end infrastructure +# rather than the end user may result in better performance. +# Docs: https://developers.cloudflare.com/workers/configuration/smart-placement/#smart-placement +# [placement] +# mode = "smart" + +# Variable bindings. These are arbitrary, plaintext strings (similar to environment variables) +# Docs: +# - https://developers.cloudflare.com/workers/wrangler/configuration/#environment-variables +# Note: Use secrets to store sensitive data. +# - https://developers.cloudflare.com/workers/configuration/secrets/ +# [vars] +# MY_VARIABLE = "production_value" + +# Bind the Workers AI model catalog. Run machine learning models, powered by serverless GPUs, on Cloudflare’s global network +# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#workers-ai +# [ai] +# binding = "AI" + +# Bind an Analytics Engine dataset. Use Analytics Engine to write analytics within your Pages Function. +# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#analytics-engine-datasets +# [[analytics_engine_datasets]] +# binding = "MY_DATASET" + +# Bind a headless browser instance running on Cloudflare's global network. +# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#browser-rendering +# [browser] +# binding = "MY_BROWSER" + +# Bind a D1 database. D1 is Cloudflare’s native serverless SQL database. +# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#d1-databases +# [[d1_databases]] +# binding = "MY_DB" +# database_name = "my-database" +# database_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" + +# Bind a dispatch namespace. Use Workers for Platforms to deploy serverless functions programmatically on behalf of your customers. +# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#dispatch-namespace-bindings-workers-for-platforms +# [[dispatch_namespaces]] +# binding = "MY_DISPATCHER" +# namespace = "my-namespace" + +# Bind a Durable Object. Durable objects are a scale-to-zero compute primitive based on the actor model. +# Durable Objects can live for as long as needed. Use these when you need a long-running "server", such as in realtime apps. +# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#durable-objects +# [[durable_objects.bindings]] +# name = "MY_DURABLE_OBJECT" +# class_name = "MyDurableObject" + +# Durable Object migrations. +# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#migrations +# [[migrations]] +# tag = "v1" +# new_classes = ["MyDurableObject"] + +# Bind a Hyperdrive configuration. Use to accelerate access to your existing databases from Cloudflare Workers. +# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#hyperdrive +# [[hyperdrive]] +# binding = "MY_HYPERDRIVE" +# id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + +# Bind a KV Namespace. Use KV as persistent storage for small key-value pairs. +# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#kv-namespaces +# [[kv_namespaces]] +# binding = "MY_KV_NAMESPACE" +# id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + +# Bind an mTLS certificate. Use to present a client certificate when communicating with another service. +# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#mtls-certificates +# [[mtls_certificates]] +# binding = "MY_CERTIFICATE" +# certificate_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" + +# Bind a Queue producer. Use this binding to schedule an arbitrary task that may be processed later by a Queue consumer. +# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#queues +# [[queues.producers]] +# binding = "MY_QUEUE" +# queue = "my-queue" + +# Bind a Queue consumer. Queue Consumers can retrieve tasks scheduled by Producers to act on them. +# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#queues +# [[queues.consumers]] +# queue = "my-queue" + +# Bind an R2 Bucket. Use R2 to store arbitrarily large blobs of data, such as files. +# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#r2-buckets +# [[r2_buckets]] +# binding = "MY_BUCKET" +# bucket_name = "my-bucket" + +# Bind another Worker service. Use this binding to call another Worker without network overhead. +# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#service-bindings +# [[services]] +# binding = "MY_SERVICE" +# service = "my-service" + +# Bind a Vectorize index. Use to store and query vector embeddings for semantic search, classification and other vector search use-cases. +# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#vectorize-indexes +# [[vectorize]] +# binding = "MY_INDEX" +# index_name = "my-index" diff --git a/examples/online-store-customer-service/online-cs-chatbot/.env.example b/examples/online-store-customer-service/online-cs-chatbot/.env.example new file mode 100644 index 00000000..85d27328 --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot/.env.example @@ -0,0 +1 @@ +NEXT_LB_PIPE_API_KEY="" \ No newline at end of file diff --git a/examples/online-store-customer-service/online-cs-chatbot/.gitignore b/examples/online-store-customer-service/online-cs-chatbot/.gitignore new file mode 100644 index 00000000..54a02eee --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot/.gitignore @@ -0,0 +1,54 @@ +# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. + +# dependencies +/node_modules +/.pnp +.pnp.js + +# testing +/coverage + +# next.js +/.next/ +/out/ + +# production +/build + +# misc +.DS_Store +*.pem + +# debug +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# local env files +.env +.env*.local +.copy.local.env +.copy.remote.env + +# vercel +.vercel + +# typescript +*.tsbuildinfo +next-env.d.ts + +# Supabase +seed.sql +xseed.sql +xxseed.sql +-seed.sql +/supabase/seed.sql +/test-results/ +/playwright-report/ +/blob-report/ +/playwright/.cache/ + +# No lock files. +package-lock.json +yarn.lock +dist diff --git a/examples/online-store-customer-service/online-cs-chatbot/app/api/chat/route.ts b/examples/online-store-customer-service/online-cs-chatbot/app/api/chat/route.ts new file mode 100755 index 00000000..1c3e4578 --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot/app/api/chat/route.ts @@ -0,0 +1,59 @@ +import { OpenAIStream, StreamingTextResponse } from 'ai' + +export const runtime = 'edge' + +/** + * Stream AI Chat Messages from Langbase + * + * @param req + * @returns + */ + +export async function POST(req: Request) { + try { + const body = await req.json() + const { messages, threadId } = body + + const response = await fetch('http://localhost:8787', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'lb-thread-id': threadId || '' + }, + body: JSON.stringify({ messages: messages }) + }) + + if (!response.ok) { + const errorMessage = await response.text(); + const status = response.status; + const errorResponse = { + error: status === 429 ? 'Rate limit exceeded' : 'Backend request failed', + details: errorMessage, + status: status + }; + return new Response(JSON.stringify(errorResponse), { + status: status, + headers: { + 'Content-Type': 'application/json' + } + }); + } + + // Create a stream from the response + const stream = OpenAIStream(response) + + // Return a streaming response + return new StreamingTextResponse(stream, { + headers: { + 'lb-thread-id': response.headers.get('lb-thread-id') || '' + } + }) + + + } catch (error: any) { + console.error('Uncaught API Error:', error) + return new Response(JSON.stringify({ error: error.message }), { + status: 500 + }) + } +} diff --git a/examples/online-store-customer-service/online-cs-chatbot/app/favicon.ico b/examples/online-store-customer-service/online-cs-chatbot/app/favicon.ico new file mode 100644 index 00000000..7c6d4a8a Binary files /dev/null and b/examples/online-store-customer-service/online-cs-chatbot/app/favicon.ico differ diff --git a/examples/online-store-customer-service/online-cs-chatbot/app/globals.css b/examples/online-store-customer-service/online-cs-chatbot/app/globals.css new file mode 100755 index 00000000..36084b74 --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot/app/globals.css @@ -0,0 +1,99 @@ +@tailwind base; +@tailwind components; +@tailwind utilities; + +/* Zinc */ +/* --background: 240 10% 3.9%; */ +/* --muted: 240 3.7% 15.9%; */ +@layer base { + :root { + --background: 0 0% 100%; + --foreground: 240 10% 3.9%; + --card: 0 0% 100%; + --card-foreground: 240 10% 3.9%; + --popover: 0 0% 100%; + --popover-foreground: 240 10% 3.9%; + --primary: 240 5.9% 10%; + --primary-foreground: 0 0% 98%; + --secondary: 240 4.8% 95.9%; + --secondary-foreground: 240 5.9% 10%; + --muted: 240 4.8% 95.9%; + --muted-foreground: 240 3.8% 46.1%; + --accent: 240 4.8% 95.9%; + --accent-foreground: 240 5.9% 10%; + /* --destructive: 0 84.2% 60.2%; */ + --destructive: 2.74 92.59% 62.94%; + --destructive-foreground: 0 0% 98%; + --warning: 46.38 70.61% 48.04%; + --warning-foreground: 120 12.5% 3.14%; + --border: 240 5.9% 90%; + --input: 240 5.9% 90%; + --ring: 240 5.9% 10%; + --radius: 6px; + --danger: 2.74 92.59% 62.94%; + } + + .dark { + /* --background: 120 12.5% 3.14%; */ + --background: 240, 3%, 9%; + --foreground: 0 0% 98%; + --card: 240 10% 3.9%; + --card-foreground: 0 0% 98%; + --popover: 240 10% 3.9%; + --popover-foreground: 0 0% 98%; + --primary: 0 0% 98%; + --primary-foreground: 240 5.9% 10%; + --secondary: 240 3.7% 15.9%; + --secondary-foreground: 0 0% 98%; + /* --muted: 165 10% 7.84%; */ + --muted: 240 3.45% 11.37%; + --muted-foreground: 240 5% 64.9%; + --accent: 240 3.7% 15.9%; + --accent-foreground: 0 0% 98%; + /* --destructive: 0 62.8% 30.6%; */ + --destructive: 356.18 70.61% 48.04%; + --destructive-foreground: 0 0% 98%; + --warning: 46.38 70.61% 48.04%; + --warning-foreground: 120 12.5% 3.14%; + /* --border: 240 3.7% 15.9%; */ + --border: 240 2% 14%; + --border-muted: 240 2% 14%; + --input: 240 3.7% 15.9%; + --ring: 240 4.9% 83.9%; + --danger: 356.18 70.61% 48.04%; + } +} + +@layer base { + * { + @apply border-border; + } + body { + @apply bg-background text-foreground; + } +} + +::selection { + color: hsl(var(--background)); + background: hsl(var(--foreground)); +} + +.google { + display: inline-block; + width: 20px; + height: 20px; + position: relative; + background-size: contain; + background-repeat: no-repeat; + background-position: 50%; + background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' viewBox='0 0 48 48'%3E%3Cdefs%3E%3Cpath id='a' d='M44.5 20H24v8.5h11.8C34.7 33.9 30.1 37 24 37c-7.2 0-13-5.8-13-13s5.8-13 13-13c3.1 0 5.9 1.1 8.1 2.9l6.4-6.4C34.6 4.1 29.6 2 24 2 11.8 2 2 11.8 2 24s9.8 22 22 22c11 0 21-8 21-22 0-1.3-.2-2.7-.5-4z'/%3E%3C/defs%3E%3CclipPath id='b'%3E%3Cuse xlink:href='%23a' overflow='visible'/%3E%3C/clipPath%3E%3Cpath clip-path='url(%23b)' fill='%23FBBC05' d='M0 37V11l17 13z'/%3E%3Cpath clip-path='url(%23b)' fill='%23EA4335' d='M0 11l17 13 7-6.1L48 14V0H0z'/%3E%3Cpath clip-path='url(%23b)' fill='%2334A853' d='M0 37l30-23 7.9 1L48 0v48H0z'/%3E%3Cpath clip-path='url(%23b)' fill='%234285F4' d='M48 48L17 24l-4-3 35-10z'/%3E%3C/svg%3E"); +} + +@keyframes spin { + from { + transform: rotate(0deg); + } + to { + transform: rotate(360deg); + } +} diff --git a/examples/online-store-customer-service/online-cs-chatbot/app/layout.tsx b/examples/online-store-customer-service/online-cs-chatbot/app/layout.tsx new file mode 100755 index 00000000..af058970 --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot/app/layout.tsx @@ -0,0 +1,36 @@ +import { Header } from '@/components/header' +import cn from 'mxcn' +import type { Metadata } from 'next' +import { Inter } from 'next/font/google' +import { Toaster } from 'sonner' +import './globals.css' + +const inter = Inter({ subsets: ['latin'] }) + +export const metadata: Metadata = { + title: 'Online Store Customer Service Chatbot - Langbase', + description: 'Build an Online Store Customer Service Chatbot with ⌘ Langbase using any LLM model.', + keywords: ['Online Store Customer Service', 'Chatbot', 'Langbase'] +} + +export default function RootLayout({ + children +}: Readonly<{ + children: React.ReactNode +}>) { + return ( + + +
+
+ +
+
+ {children} +
+
+
+ + + ) +} diff --git a/examples/online-store-customer-service/online-cs-chatbot/app/page.tsx b/examples/online-store-customer-service/online-cs-chatbot/app/page.tsx new file mode 100755 index 00000000..f278d230 --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot/app/page.tsx @@ -0,0 +1,7 @@ +import { Chatbot } from '@/components/chatbot-page' + +export const runtime = 'edge' + +export default function ChatPage() { + return +} diff --git a/examples/online-store-customer-service/online-cs-chatbot/components/chat-input.tsx b/examples/online-store-customer-service/online-cs-chatbot/components/chat-input.tsx new file mode 100755 index 00000000..569fbc6f --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot/components/chat-input.tsx @@ -0,0 +1,75 @@ +import { type UseChatHelpers } from 'ai/react' + +import { PromptForm } from '@/components/prompt-form' +import { Button } from '@/components/ui/button' +import { IconRegenerate, IconStop } from '@/components/ui/icons' + +export interface ChatInputProps + extends Pick< + UseChatHelpers, + | 'append' + | 'isLoading' + | 'reload' + | 'messages' + | 'stop' + | 'input' + | 'setInput' + > { + id?: string +} + +export function ChatInput({ + id, + isLoading, + stop, + append, + reload, + input, + setInput, + messages +}: ChatInputProps) { + return ( +
+
+
+ {isLoading ? ( + + ) : ( + messages?.length > 0 && ( + + ) + )} +
+
+ { + await append({ + content: value, + role: 'user' + }) + }} + input={input} + setInput={setInput} + isLoading={isLoading} + /> +
+
+
+ ) +} diff --git a/examples/online-store-customer-service/online-cs-chatbot/components/chat-list.tsx b/examples/online-store-customer-service/online-cs-chatbot/components/chat-list.tsx new file mode 100755 index 00000000..32074928 --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot/components/chat-list.tsx @@ -0,0 +1,27 @@ +import { type Message } from 'ai' + +import { Separator } from '@/components/ui/separator' +import { ChatMessage } from '@/components/chat-message' + +export interface ChatList { + messages: Message[] +} + +export function ChatList({ messages }: ChatList) { + if (!messages.length) { + return null + } + + return ( +
+ {messages.map((message, index) => ( +
+ + {index < messages.length - 1 && ( + + )} +
+ ))} +
+ ) +} diff --git a/examples/online-store-customer-service/online-cs-chatbot/components/chat-message-actions.tsx b/examples/online-store-customer-service/online-cs-chatbot/components/chat-message-actions.tsx new file mode 100755 index 00000000..37f68b2a --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot/components/chat-message-actions.tsx @@ -0,0 +1,40 @@ +'use client' + +import { type Message } from 'ai' + +import { Button } from '@/components/ui/button' +import { IconCheck, IconCopy } from '@/components/ui/icons' +import { useCopyToClipboard } from '@/lib/hooks/use-copy-to-clipboard' +import cn from 'mxcn' + +interface ChatMessageActionsProps extends React.ComponentProps<'div'> { + message: Message +} + +export function ChatMessageActions({ + message, + className, + ...props +}: ChatMessageActionsProps) { + const { isCopied, copyToClipboard } = useCopyToClipboard({ timeout: 2000 }) + + const onCopy = () => { + if (isCopied) return + copyToClipboard(message.content) + } + + return ( +
+ +
+ ) +} diff --git a/examples/online-store-customer-service/online-cs-chatbot/components/chat-message.tsx b/examples/online-store-customer-service/online-cs-chatbot/components/chat-message.tsx new file mode 100755 index 00000000..c359618d --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot/components/chat-message.tsx @@ -0,0 +1,77 @@ +import { Message } from 'ai' +import remarkGfm from 'remark-gfm' +import remarkMath from 'remark-math' + +import { ChatMessageActions } from '@/components/chat-message-actions' +import { MemoizedReactMarkdown } from '@/components/markdown' +import { CodeBlock } from '@/components/ui/codeblock' +import { IconSparkles, IconUser } from '@/components/ui/icons' +import cn from 'mxcn' + +export interface ChatMessageProps { + message: Message +} + +export function ChatMessage({ message, ...props }: ChatMessageProps) { + return ( +
+
+ {message.role === 'user' ? : } +
+
+ {children}

+ }, + code({ node, inline, className, children, ...props }) { + if (children.length) { + if (children[0] == '▍') { + return ( + ▍ + ) + } + + children[0] = (children[0] as string).replace('`▍`', '▍') + } + + const match = /language-(\w+)/.exec(className || '') + + if (inline) { + return ( + + {children} + + ) + } + + return ( + + ) + } + }} + > + {message.content} +
+ +
+
+ ) +} diff --git a/examples/online-store-customer-service/online-cs-chatbot/components/chatbot-page.tsx b/examples/online-store-customer-service/online-cs-chatbot/components/chatbot-page.tsx new file mode 100755 index 00000000..d79ee24e --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot/components/chatbot-page.tsx @@ -0,0 +1,69 @@ +'use client' + +import { ChatList } from '@/components/chat-list' +import { useChat, type Message } from 'ai/react' +import cn from 'mxcn' +import { useState } from 'react' +import { toast } from 'sonner' +import { ChatInput } from './chat-input' +import { Opening } from './opening' +import { Suggestions } from './suggestions' + +export interface ChatProps extends React.ComponentProps<'div'> { + id?: string // Optional: Thread ID if you want to persist the chat in a DB + initialMessages?: Message[] // Optional: Messages to pre-populate the chat from DB +} + +export function Chatbot({ id, initialMessages, className }: ChatProps) { + const [threadId, setThreadId] = useState(null) + const { messages, append, reload, stop, isLoading, input, setInput } = + useChat({ + api: '/api/chat', + initialMessages, + body: { threadId }, + onResponse(response) { + if (response.status !== 200) { + response.json().then(data => { + toast.error(data.error || response.statusText) + }).catch(() => { + toast.error(response.statusText) + }) + } + + // Get Thread ID from response header + const lbThreadId = response.headers.get('lb-thread-id') + setThreadId(lbThreadId) + } + }) + + const sendSuggestedPrompt = (prompt: string) => { + setInput(prompt) + } + + return ( +
+
+ {messages.length ? ( + <> + + + ) : ( + <> + + + + )} +
+ +
+ ) +} diff --git a/examples/online-store-customer-service/online-cs-chatbot/components/header.tsx b/examples/online-store-customer-service/online-cs-chatbot/components/header.tsx new file mode 100755 index 00000000..d696362e --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot/components/header.tsx @@ -0,0 +1,47 @@ +import { buttonVariants } from '@/components/ui/button' +import cn from 'mxcn' +import Link from 'next/link' +import { IconFork, IconGitHub } from './ui/icons' + +export async function Header() { + return ( +
+
+

+ + + Langbase + +

+
+ + +
+ ) +} diff --git a/examples/online-store-customer-service/online-cs-chatbot/components/markdown.tsx b/examples/online-store-customer-service/online-cs-chatbot/components/markdown.tsx new file mode 100755 index 00000000..d4491467 --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot/components/markdown.tsx @@ -0,0 +1,9 @@ +import { FC, memo } from 'react' +import ReactMarkdown, { Options } from 'react-markdown' + +export const MemoizedReactMarkdown: FC = memo( + ReactMarkdown, + (prevProps, nextProps) => + prevProps.children === nextProps.children && + prevProps.className === nextProps.className +) diff --git a/examples/online-store-customer-service/online-cs-chatbot/components/opening.tsx b/examples/online-store-customer-service/online-cs-chatbot/components/opening.tsx new file mode 100755 index 00000000..a7ffc259 --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot/components/opening.tsx @@ -0,0 +1,89 @@ +import Link from 'next/link' + +export function Opening() { + return ( +
+
+
+
+ + Chatbot Example + +
+ +
+
+

+ Online Store (TechBay) Customer Service AI Agent by a + + pipe on ⌘ Langbase + +

+
+ Ship hyper-personalized AI assistants with memory. +
+
+ +
+

Online Store (TechBay) Customer Service example consists of three pipes checkout the following links:

+
+ + 1. + Fork ticket resolution AI Agent for electronics section Pipe on ⌘ Langbase + + + 2. + Fork ticket resolution AI Agent for sports gear section Pipe on ⌘ Langbase + + + 3. + Fork ticket resolution AI Agent for travel bags section Pipe on ⌘ Langbase + + + 4. + Use LangUI.dev's open source code components + + + + 5. + Go through Documentaion: Pipe Quickstart + + + 6. + + Learn more about Pipes & Memory features on ⌘ Langbase + + +
+
+
+
+
+ ) +} + +// Description Link +function Dlink({ + href, + children, + ...props +}: { + href: string + children: React.ReactNode + [key: string]: any +}) { + return ( + + {children} + + ) +} diff --git a/examples/online-store-customer-service/online-cs-chatbot/components/prompt-form.tsx b/examples/online-store-customer-service/online-cs-chatbot/components/prompt-form.tsx new file mode 100755 index 00000000..49b6cb77 --- /dev/null +++ b/examples/online-store-customer-service/online-cs-chatbot/components/prompt-form.tsx @@ -0,0 +1,95 @@ +import { Button } from '@/components/ui/button' +import { IconChat, IconCommand, IconSpinner } from '@/components/ui/icons' +import { useEnterSubmit } from '@/lib/hooks/use-enter-submit' +import { UseChatHelpers } from 'ai/react' +import * as React from 'react' +import Textarea from 'react-textarea-autosize' + +export interface PromptProps + extends Pick { + onSubmit: (value: string) => Promise + isLoading: boolean +} + +export function PromptForm({ + onSubmit, + input, + setInput, + isLoading +}: PromptProps) { + const { formRef, onKeyDown } = useEnterSubmit() + const inputRef = React.useRef(null) + + React.useEffect(() => { + if (inputRef.current) { + inputRef.current.focus() + } + }, []) + + return ( +
{ + e.preventDefault() + if (!input?.trim()) { + return + } + setInput('') + await onSubmit(input) + }} + ref={formRef} + > +
+
+ +
+