
AI Travel Agent
Deprecated
Pricing
Pay per event
Go to Store


AI Travel Agent
Deprecated
The AI Travel Agent is designed to assist users in planning their perfect trip through a simple conversational interface. With this agent, users can input specific criteria for their desired destination, such as neighborhood and budget, and receive tailored suggestions.
0.0 (0)
Pricing
Pay per event
1
Total users
3
Monthly users
3
Runs succeeded
>99%
Last modified
a month ago
.dockerignore
# configurations.idea.vscode
# crawlee and apify storage foldersapify_storagecrawlee_storagestorage
# installed filesnode_modules
# git folder.git
# dist folderdist
.editorconfig
root = true
[*]indent_style = spaceindent_size = 2charset = utf-8trim_trailing_whitespace = trueinsert_final_newline = trueend_of_line = lf
.env.example
USER_APIFY_TOKEN="apify_api_1234"ACTOR_TEST_PAY_PER_EVENT=trueLANGSMITH_TRACING=falseLANGSMITH_ENDPOINT="https://api.smith.langchain.com"LANGSMITH_API_KEY="lsv2_pt_1234"LANGSMITH_PROJECT="langsmith-cool-project-name"OPENAI_API_KEY="sk-proj-1234"
.eslintrc
{ "root": false, "env": { "browser": true, "es2020": true, "node": true }, "extends": [ "@apify/eslint-config-ts" ], "parserOptions": { "project": "./tsconfig.json", "ecmaVersion": 2020 }, "ignorePatterns": [ "node_modules", "dist", "**/*.d.ts" ], "rules": { "indent": ["error", 2], "comma-dangle": ["off", 0], "prefer-rest-params": ["off", 0], "max-len": ["error", { "ignoreComments": true, "ignoreStrings": true, "ignoreUrls": true, "ignoreTemplateLiterals": true }] }}
.gitignore
# This file tells Git which files shouldn't be added to source control
.idea.vscodestorageapify_storagecrawlee_storagenode_modulesdisttsconfig.tsbuildinfo.env
# Added by Apify CLI.venv
.gitpod.yml
image: gitpod/workspace-full:latesttasks: - name: main init: > nvm use lts/jod && npm install -g npm@11 && npm install -g eslint@^8.50.0 && npm install -g apify-cli command: echo "Login to apify to get an API Key, export your APIFY_TOKEN, create an INPUT.json file and run npm start!" - name: config before: > (([[ ! -z $GITCONFIG ]] && echo $GITCONFIG | base64 -d > ~/.gitconfig && chmod 644 ~/.gitconfig) || unset GITCONFIG) && (([[ ! -z $GNUPG_1 ]] && rm -rf ~/.gnupg && cd / && echo $GNUPG_1$GNUPG_2 | base64 -d | tar --no-same-owner -xzf -) || unset GNUPG_1) command: echo "Ready! Start a new terminal session in order to use GPG." && exit
LICENSE
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
(a) You must give any other recipients of the Work or Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
environment.d.ts
1declare global {2 namespace NodeJS {3 interface ProcessEnv {4 USER_APIFY_TOKEN: string;5 LANGSMITH_TRACING: string;6 LANGSMITH_ENDPOINT: string;7 LANGSMITH_API_KEY: string;8 LANGSMITH_PROJECT: string;9 OPENAI_API_KEY: string;10 ZIP_API_KEY: string;11 NODE_ENV: 'development' | 'production';12 }13 }14}15
16// If this file has no import/export statements (i.e. is a script)17// convert it into a module by adding an empty export statement.18export {}
mermaid.png
package.json
{ "name": "ai-travel-agent", "version": "0.0.1", "type": "module", "description": "AI Agent Actor that leverages Apify to search for travel opportunities.", "engines": { "node": ">=22.0.0" }, "dependencies": { "@langchain/core": "^0.3.42", "@langchain/langgraph": "^0.2.54", "@langchain/openai": "^0.4.4", "apify": "^3.3.2", "crypto": "^1.0.1", "dotenv": "^16.4.7", "langchain": "^0.3.19", "zod": "^3.24.2" }, "devDependencies": { "@apify/eslint-config-ts": "^0.3.0", "@apify/tsconfig": "^0.1.0", "@eslint/js": "^9.21.0", "@types/node": "^22.0.0", "@typescript-eslint/eslint-plugin": "^7.18.0", "@typescript-eslint/parser": "^7.18.0", "eslint": "^8.57.1", "globals": "^16.0.0", "tsx": "^4.4.0", "typescript": "~5.5.0", "typescript-eslint": "^8.26.0" }, "scripts": { "start": "npm run start:dev", "start:prod": "node dist/main.js", "start:dev": "tsx src/main.ts", "build": "tsc", "lint": "eslint src", "test": "echo \"Error: oops, the actor has no tests yet, sad!\" && exit 1" }, "author": "It's not you it's me", "license": "ISC"}
tsconfig.json
{ "extends": "@apify/tsconfig", "compilerOptions": { "module": "NodeNext", "moduleResolution": "NodeNext", "target": "ES2022", "outDir": "dist", "noUnusedLocals": false, "skipLibCheck": true, "lib": ["DOM"], }, "include": [ "./*.d.ts", "./src/**/*", ]}
.actor/Dockerfile
# Specify the base Docker image. You can read more about# the available images at https://docs.apify.com/sdk/js/docs/guides/docker-images# You can also use any other image from Docker Hub.FROM apify/actor-node:22 AS builder
# Check preinstalled packagesRUN npm ls crawlee apify puppeteer playwright
# Copy just package.json and package-lock.json# to speed up the build using Docker layer cache.COPY package*.json ./
# Install all dependencies. Don't audit to speed up the installation.RUN npm install --include=dev --audit=false
# Next, copy the source files using the user set# in the base image.COPY . ./
# Install all dependencies and build the project.# Don't audit to speed up the installation.RUN npm run build
# Create final imageFROM apify/actor-node:22
# Check preinstalled packagesRUN npm ls crawlee apify puppeteer playwright
# Copy just package.json and package-lock.json# to speed up the build using Docker layer cache.COPY package*.json ./
# Install NPM packages, skip optional and development dependencies to# keep the image small. Avoid logging too much and print the dependency# tree for debuggingRUN npm --quiet set progress=false \ && npm install --omit=dev --omit=optional \ && echo "Installed NPM packages:" \ && (npm list --omit=dev --all || true) \ && echo "Node.js version:" \ && node --version \ && echo "NPM version:" \ && npm --version \ && rm -r ~/.npm
# Copy built JS files from builder imageCOPY /usr/src/app/dist ./dist
# Next, copy the remaining files and directories with the source code.# Since we do this after NPM install, quick build will be really fast# for most source file changes.COPY . ./
# Create and run as a non-root user.RUN adduser -h /home/apify -D apify && \ chown -R apify:apify ./USER apify
# Run the image.CMD npm run start:prod --silent
.actor/actor.json
{ "actorSpecification": 1, "name": "ai-travel-agent", "version": "0.1", "buildTag": "latest", "title": "AI Travel Agent", "description": "AI agent that uses a multi-agent approach to find you the travel recommendations using a natural language prompt.", "meta": { "templateId": "ts-empty" }, "environmentVariables": { "OPENAI_API_KEY": "@defaultOpenaiApiKey", "USER_APIFY_TOKEN": "@defaultApifyToken" }, "dockerfile": "./Dockerfile", "input": "./input_schema.json", "storages": { "dataset": "./dataset_schema.json" }}
.actor/dataset_schema.json
{ "actorSpecification": 1, "views": { "overview": { "title": "Overview", "transformation": { "fields": ["actorName", "response"] }, "display": { "component": "table", "properties": { "actorName": { "label": "Actor name", "format": "text" }, "response": { "label": "Actor evaluation", "format": "text" } } } } }}
.actor/input_schema.json
{ "title": "Web Automation Agent", "type": "object", "schemaVersion": 1, "properties": { "instructions": { "title": "Instructions for the AI Travel Agent", "type": "string", "description": "Ask the agent for help to plan the perfect trip.", "editor": "textarea", "prefill": "I want to buy a house with a pool in Miami for less than 1 million dollars. Can you help me?" }, "openaiApiKey": { "title": "OpenAI API key", "type": "string", "description": "The API key for accessing OpenAI. You can get it from <a href='https://platform.openai.com/account/api-keys' target='_blank' rel='noopener'>OpenAI platform</a>.", "editor": "textfield", "isSecret": true }, "model": { "title": "GPT model", "type": "string", "description": "Select a GPT model. See <a href='https://platform.openai.com/docs/models/overview' target='_blank' rel='noopener'>models overview</a>. Keep in mind that each model has different pricing and features.", "editor": "select", "default": "gpt-4o-mini", "prefill": "gpt-4o-mini", "enum": ["gpt-4o", "gpt-4o-mini", "gpt-3.5-turbo"] }, "debug": { "title": "Debug Mode", "type": "boolean", "description": "Mark this option as true if you want to see all the logs when running the actor.", "editor": "checkbox", "default": false } }, "required": ["instructions", "model"]}
.actor/pay_per_event.json
{ "actor-start-gb": { "eventTitle": "Actor start per 1 GB", "eventDescription": "Flat fee for starting an Actor run for each 1 GB of memory.", "eventPriceUsd": 0.01 }, "openai-1000-tokens-gpt-4o": { "eventTitle": "Price per 1000 OpenAI tokens for gpt-4o", "eventDescription": "Flat fee for every 1000 tokens (input/output) used with gpt-4o.", "eventPriceUsd": 0.01 }, "openai-1000-tokens-gpt-4o-mini": { "eventTitle": "Price per 1000 OpenAI tokens for gpt-4o-mini", "eventDescription": "Flat fee for every 1000 tokens (input/output) used with gpt-4o-mini.", "eventPriceUsd": 0.0006 }, "openai-1000-tokens-gpt-3-5-turbo": { "eventTitle": "Price per 1000 OpenAI tokens for gpt-3.5-turbo", "eventDescription": "Flat fee for every 1000 tokens (input/output) used with gpt-3.5-turbo.", "eventPriceUsd": 0.0015 }, "duck-duck-go": { "eventTitle": "Price per Duck Duck Go search", "eventDescription": "Flat fee for every Duck Duck Go search.", "eventPriceUsd": 0.01 }, "website-scraper':": { "eventTitle": "Price per web page scraped", "eventDescription": "Flat fee for every web page scraped.", "eventPriceUsd": 0.01 }, "airbnb-search':": { "eventTitle": "Price per Airbnb search result", "eventDescription": "Flat fee for every result when searching Airbnb.", "eventPriceUsd": 0.001 }, "tripadvisor-search':": { "eventTitle": "Price per TripAdvisor search result", "eventDescription": "Flat fee for every result when searching TripAdvisor.", "eventPriceUsd": 0.003 }}
src/main.ts
1import dotenv from 'dotenv';2import { Actor, ApifyClient, log } from 'apify';3import { StateGraph, StateGraphArgs, START, END } from '@langchain/langgraph';4import LocationAgent from './agents/location.js';5import DeciderAgent from './agents/decider.js';6import ResearcherAgent from './agents/researcher.js';7import SuccessAgent from './agents/success.js';8import { chargeForActorStart } from './utils/ppe_handler.js';9
10// if available, .env variables are loaded11dotenv.config();12
13/**14 * Actor input schema15 */16type Input = {17 instructions: string;18 modelName?: string;19 openaiApiKey?: string;20 debug?: boolean;21}22
23/**24 * Actor initialization code and initial charge25*/26await Actor.init();27await chargeForActorStart();28
29// Handle and validate input30const {31 instructions,32 modelName = 'gpt-4o-mini',33 openaiApiKey,34 debug,35} = await Actor.getInput() as Input;36if (debug) {37 log.setLevel(log.LEVELS.DEBUG);38} else {39 log.setLevel(log.LEVELS.INFO);40}41// if available, the Actor uses the user's openaiApiKey. Otherwise it charges for use.42const tokenCostActive = (openaiApiKey ?? '').length === 0;43if (tokenCostActive) {44 log.info("No openaiApiKey was detected. You'll be charged for token usage.");45} else {46 log.info("Env openaiApiKey detected. You won't be charged for token usage.");47}48if (!instructions) {49 throw new Error('Instructions are required. Create an INPUT.json file in the `storage/key_value_stores/default` folder and add the respective keys.');50}51
52// Apify is used to call tools and manage datasets53const userToken = process.env.USER_APIFY_TOKEN;54if (!userToken) {55 throw new Error('User token is required. Export your Apify secret as USER_APIFY_TOKEN.');56}57const apifyClient = new ApifyClient({58 token: userToken,59});60
61/**62 * LangGraph StateGraph schema63 */64type StateSchema = {65 instructions: string;66 bestLocations: string;67 datasetId: string;68 totalItems: number;69 assumptions: string;70 itemsChecked: number;71 recommendations: string[];72 output: string;73}74
75const graphState: StateGraphArgs<StateSchema>['channels'] = {76 instructions: {77 value: (x?: string, y?: string) => y ?? x ?? '',78 default: () => instructions,79 },80 bestLocations: {81 value: (x?: string, y?: string) => y ?? x ?? '',82 default: () => '',83 },84 datasetId: {85 value: (x?: string, y?: string) => y ?? x ?? '',86 default: () => '',87 },88 totalItems: {89 value: (x?: number, y?: number) => y ?? x ?? 0,90 default: () => 0,91 },92 assumptions: {93 value: (x?: string, y?: string) => y ?? x ?? '',94 default: () => '',95 },96 itemsChecked: {97 value: (x?: number, y?: number) => y ?? x ?? 0,98 default: () => 0,99 },100 recommendations: {101 value: (x?: string[], y?: string[]) => y ?? x ?? [],102 default: () => [],103 },104 output: {105 value: (x?: string, y?: string) => y ?? x ?? '',106 default: () => '',107 },108};109
110async function locationNode(state: StateSchema) {111 const locationAgent = new LocationAgent({112 apifyClient,113 modelName,114 openaiApiKey: openaiApiKey ?? process.env.OPENAI_API_KEY,115 log,116 });117 const { agentExecutor, costHandler } = locationAgent;118 const response = await agentExecutor.invoke({ input: state.instructions });119 log.debug(`locationAgent 🤖 : ${response.output}`);120 await costHandler.logOrChargeForTokens(modelName, tokenCostActive);121 return { bestLocations: response.output };122}123
124async function researcherNode(state: StateSchema) {125 const researcherAgent = new ResearcherAgent({126 apifyClient,127 modelName,128 openaiApiKey: openaiApiKey ?? process.env.OPENAI_API_KEY,129 log,130 });131 const { agentExecutor, costHandler } = researcherAgent;132 const input = 'The user asked sent this exact query:\n\n'133 + `'${state.instructions}'\n\n`134 + 'You asked someone for help to get the best locations in that city and country. '135 + 'The answer you received was this:\n\n'136 + `'${state.bestLocations}'\n\n`137 + 'Please make some research to gather information to be able to answer the user accordingly.';138 const response = await agentExecutor.invoke({ input });139 log.debug(`researcherAgent 🤖 : ${response.output}`);140 const { datasetId, totalItems, assumptions } = JSON.parse(response.output);141 await costHandler.logOrChargeForTokens(modelName, tokenCostActive);142 return { datasetId, totalItems, assumptions };143}144
145async function deciderNode(state: StateSchema) {146 const deciderAgent = new DeciderAgent({147 apifyClient,148 modelName,149 openaiApiKey: openaiApiKey ?? process.env.OPENAI_API_KEY,150 log,151 });152 const { agentExecutor, costHandler } = deciderAgent;153 const input = 'The user asked sent this exact query:\n\n'154 + `'${state.instructions}'\n\n`155 + 'You asked someone for help to get the best locations in that city and country. '156 + 'The answer you received was this:\n\n'157 + `'${state.bestLocations}'\n\n`158 + 'You asked someone else for help to get the best places to stay in in those locations using Airbnb. '159 + 'The answer you received was this:\n\n'160 + `Airbnb Apify dataset ID: '${state.datasetId}'\n`161 + `Total items in Airbnb dataset: '${state.totalItems}'\n`162 + `Assumptions made when cretating the Airbnb dataset:: '${state.assumptions}'\n`163 + `Total results already checked in Airbnb dataset: itemsChecked='${state.itemsChecked}'\n\n`164 + 'Please explore theavailable datasets for the best results.';165 const response = await agentExecutor.invoke({ input });166 log.debug(`deciderAgent 🤖 : ${response.output}`);167 const { itemsChecked, recommendations } = JSON.parse(response.output);168 await costHandler.logOrChargeForTokens(modelName, tokenCostActive);169 return {170 itemsChecked: state.itemsChecked + itemsChecked,171 recommendations: [...state.recommendations, ...recommendations]172 };173}174
175const resultsCheckedRouter = (state: StateSchema) => {176 const allResultsChecked = state.itemsChecked >= state.totalItems;177 log.debug(`allResultsChecked: ${allResultsChecked}`);178 const thousandResultsChecked = state.itemsChecked > 1000;179 log.debug(`thousandResultsChecked: ${thousandResultsChecked}`);180 const fifteenRecommendationsFound = state.recommendations.length > 15;181 log.debug(`fifteenRecommendationsFound: ${fifteenRecommendationsFound}`);182 if (183 allResultsChecked184 || thousandResultsChecked185 || fifteenRecommendationsFound186 ) {187 // if any of the above criteria are met, it passes the ball to the successAgent188 return 'success';189 }190 // if none of the above criteria are met, it runs the same node again191 return 'decider';192};193
194async function successNode(state: StateSchema) {195 const successAgent = new SuccessAgent({196 apifyClient,197 modelName,198 openaiApiKey: openaiApiKey ?? process.env.OPENAI_API_KEY,199 log,200 });201 const { agentExecutor, costHandler } = successAgent;202 const input = 'The user asked sent this exact query:\n\n'203 + `'${state.instructions}'\n\n`204 + 'You asked someone for help to get the best locations in that city and state. '205 + 'The answer you received was this:\n\n'206 + `'${state.bestLocations}'\n\n`207 + 'You asked someone else for help to get the best places to stay in those locations using Airbnb and their expert jugdgement. '208 + 'The answer you received was this:\n\n'209 + `Assumptions made when searching Airbnb: '${state.assumptions}'\n\n`210 + `Total places to stay checked: '${state.itemsChecked}'\n`211 + `Total places to stay recommended: '${state.recommendations.length}'\n`212 + `Best places to stay in stringified JSON format: '${JSON.stringify(state.recommendations)}'\n`213 + 'Please select the top 5 places to stay and answer the user explaining the whole process in markdown format.';214 const response = await agentExecutor.invoke({ input });215 log.debug(`successNode 🤖 : ${response.output}`);216 await costHandler.logOrChargeForTokens(modelName, tokenCostActive);217 return { output: response.output };218}219
220const graph = new StateGraph({ channels: graphState })221 .addNode('location', locationNode)222 .addNode('researcher', researcherNode)223 .addNode('decider', deciderNode)224 .addNode('success', successNode)225 .addEdge(START, 'location')226 .addEdge('location', 'researcher')227 .addEdge('researcher', 'decider')228 .addConditionalEdges('decider', resultsCheckedRouter)229 .addEdge('success', END);230
231const runnable = graph.compile();232
233const response = await runnable.invoke(234 { input: instructions },235 { configurable: { thread_id: 42 } }, // this line shows that the agent can be thread-aware236);237
238log.debug(`Agent 🤖 : ${response.output}`);239
240await Actor.pushData({241 actorName: 'AI Travel Agent',242 response: response.output,243});244
245await Actor.exit();
src/agents/decider.ts
1import { Log, ApifyClient } from 'apify';2import { ChatPromptTemplate } from '@langchain/core/prompts';3import { createToolCallingAgent, AgentExecutor } from 'langchain/agents';4import { ChatOpenAI } from '@langchain/openai';5import { StructuredToolInterface } from '@langchain/core/tools';6import DatasetExplorer from '../tools/dataset_explorer.js';7import { CostHandler } from '../utils/cost_handler.js';8
9/**10 * Interface for parameters required by PropertyAgent class.11 */12export interface PropertyAgentParams {13 apifyClient: ApifyClient,14 modelName: string,15 openaiApiKey: string,16 log: Log | Console;17}18
19/**20 * An AI Agent that explores an Apify dataset in search for the perfect results.21 */22export class PropertyAgent {23 protected log: Log | Console;24 protected apifyClient: ApifyClient;25 public agentExecutor: AgentExecutor;26 public costHandler: CostHandler;27
28 constructor(fields?: PropertyAgentParams) {29 this.log = fields?.log ?? console;30 this.apifyClient = fields?.apifyClient ?? new ApifyClient();31 this.costHandler = new CostHandler(fields?.modelName ?? 'gpt-4o-mini', this.log);32 const llm = new ChatOpenAI({33 model: fields?.modelName,34 apiKey: fields?.openaiApiKey,35 temperature: 0,36 callbacks: [37 this.costHandler,38 ],39 });40 const tools = this.buildTools(this.apifyClient, this.log);41 const prompt = this.buildPrompt();42 const agent = createToolCallingAgent({43 llm,44 tools,45 prompt,46 });47 this.agentExecutor = new AgentExecutor({48 agent,49 tools,50 verbose: false,51 maxIterations: 5,52 });53 }54
55 protected buildTools(56 apifyClient: ApifyClient, log: Log | Console57 ): StructuredToolInterface[] {58 // Tools are initialized to be passed to the agent59 const datasetExplorer = new DatasetExplorer({ apifyClient, log });60 return [61 datasetExplorer,62 ];63 }64
65 protected buildPrompt(): ChatPromptTemplate {66 return ChatPromptTemplate.fromMessages([67 ['system',68 'You are a experienced travel agent that wants to help the user plan the perfect trip. '69 + "You'll have a datasetId and the total amount of items available to explore. "70 + 'Explore only one batch of 25 results (using limit=25). '71 + 'Skip the first items based on the number of items that were already checked (using offset=itemsChecked). '72 + "When exploring the dataset, include only these fields exactly as written here: ['id','name','title','rating','pricing','url','images']"73 + 'Remember: do not call the DatasetExplorer tool more than once, or you will die. \n\n'74 + 'The user may have specified other requests like pets, bedrooms, beds, baths, etc. '75 + 'Filter the results based on this information or notify the user if you are unable to do so. '76 + 'Use your expertise to recommend the best results that you can find that matches the user criteria. '77 + 'If the total amount of items (totalItems) is over 100, select up to 2 results otherwise pick up to 3. Try to at least pick one. '78 + "As your response, return only a JSON object (and nothing more! not even a ```json or ``` wrapper) with the key 'itemsChecked' as a number with the amount of results that you received from the dataset_explorer and the key 'recommendations' with your recommendations in JSON format, using the same fields that you specified when calling the dataset_explorer."79 ],80 ['placeholder', '{chat_history}'],81 ['human', '{input}'],82 ['placeholder', '{agent_scratchpad}'],83 ]);84 }85}86
87export default PropertyAgent;
src/agents/location.ts
1import { Log, ApifyClient } from 'apify';2import { ChatPromptTemplate } from '@langchain/core/prompts';3import { createToolCallingAgent, AgentExecutor } from 'langchain/agents';4import { ChatOpenAI } from '@langchain/openai';5import { StructuredToolInterface } from '@langchain/core/tools';6import DuckDuckGo from '../tools/duck_duck_go.js';7import WebsiteScraper from '../tools/website_scraper.js';8import { CostHandler } from '../utils/cost_handler.js';9
10/**11 * Interface for parameters required by LocationAgent class.12 */13export interface LocationAgentParams {14 apifyClient: ApifyClient,15 modelName: string,16 openaiApiKey: string,17 log: Log | Console;18}19
20/**21 * AI Agent that takes the user input and finds the best Zip Codes in a city to live in.22 */23export class LocationAgent {24 protected log: Log | Console;25 protected apifyClient: ApifyClient;26 public agentExecutor: AgentExecutor;27 public costHandler: CostHandler;28
29 constructor(fields?: LocationAgentParams) {30 this.log = fields?.log ?? console;31 this.apifyClient = fields?.apifyClient ?? new ApifyClient();32 this.costHandler = new CostHandler(fields?.modelName ?? 'gpt-4o-mini', this.log);33 const llm = new ChatOpenAI({34 model: fields?.modelName,35 apiKey: fields?.openaiApiKey,36 temperature: 0,37 callbacks: [38 this.costHandler,39 ],40 });41 const tools = this.buildTools(this.apifyClient, this.log);42 const prompt = this.buildPrompt();43 const agent = createToolCallingAgent({44 llm,45 tools,46 prompt,47 });48 this.agentExecutor = new AgentExecutor({49 agent,50 tools,51 verbose: false,52 maxIterations: 5,53 });54 }55
56 protected buildTools(57 apifyClient: ApifyClient, log: Log | Console58 ): StructuredToolInterface[] {59 // Tools are initialized to be passed to the agent60 const duckDuckGo = new DuckDuckGo({ apifyClient, log });61 const websiteScraper = new WebsiteScraper({ apifyClient, log });62 return [63 duckDuckGo,64 websiteScraper,65 ];66 }67
68 protected buildPrompt(): ChatPromptTemplate {69 return ChatPromptTemplate.fromMessages([70 ['system',71 'You are a experienced travel agent that wants to help the user plan their trip. '72 + "You will stick to the following steps, ignoring the specifics of the user's query and focusing only on finding the best locations. "73 + 'Step 1. The user will ask you for advice regarding traveling to a specific city in the world or even a neighborhood in a city. '74 + "You don't care where the user lives, since you are not in charge of flights. "75 + 'If the user does not specify a budget, select mid range (and not low budget or luxury). '76 + 'If the user does not provide a country, try to guess to which country the city belongs to. '77 + 'If from the input you cannot get a city and a country or you think that the specified city should not be visited by tourists, '78 + 'end the conversation and help the user with the input. '79 // 2. Fetch best neighborhoods to stay at using DuckDuckGo80 + 'Step 2. With the city and state in hand, use DuckDuckGo replacing the following query: '81 + "'where to stay and where not to stay in [city], [country]'. "82 + 'If there the results, use a website scraper on the URLs you think will show you this information. '83 + 'Iterate until you find the best 3 locations or neighborhoods in the selected city. '84 + "For best performance when using the website scraper, you can use method='getBestPlacesToStay' and output=null'. "85 + 'Instead of answering the original question, just return the top 3 locations or neighborhoods and explain to the user why you selected those locations or neighborhoods. \n'86 ],87 ['placeholder', '{chat_history}'],88 ['human', '{input}'],89 ['placeholder', '{agent_scratchpad}'],90 ]);91 }92}93
94export default LocationAgent;
src/agents/researcher.ts
1import { Log, ApifyClient } from 'apify';2import { ChatPromptTemplate } from '@langchain/core/prompts';3import { createToolCallingAgent, AgentExecutor } from 'langchain/agents';4import { ChatOpenAI } from '@langchain/openai';5import { StructuredToolInterface } from '@langchain/core/tools';6import AirbnbSearch from '../tools/airbnb_search.js';7import { CostHandler } from '../utils/cost_handler.js';8
9/**10 * Interface for parameters required by ResearcherAgent class.11 */12export interface ResearcherAgentParams {13 apifyClient: ApifyClient,14 modelName: string,15 openaiApiKey: string,16 log: Log | Console;17}18
19/**20 * An AI Agent that searches Researcher for specific locations and stores the results in a dataset.21 */22export class ResearcherAgent {23 protected log: Log | Console;24 protected apifyClient: ApifyClient;25 public agentExecutor: AgentExecutor;26 public costHandler: CostHandler;27
28 constructor(fields?: ResearcherAgentParams) {29 this.log = fields?.log ?? console;30 this.apifyClient = fields?.apifyClient ?? new ApifyClient();31 this.costHandler = new CostHandler(fields?.modelName ?? 'gpt-4o-mini', this.log);32 const llm = new ChatOpenAI({33 model: fields?.modelName,34 apiKey: fields?.openaiApiKey,35 temperature: 0,36 callbacks: [37 this.costHandler,38 ],39 });40 const tools = this.buildTools(this.apifyClient, this.log);41 const prompt = this.buildPrompt();42 const agent = createToolCallingAgent({43 llm,44 tools,45 prompt,46 });47 this.agentExecutor = new AgentExecutor({48 agent,49 tools,50 verbose: false,51 maxIterations: 3,52 });53 }54
55 protected buildTools(56 apifyClient: ApifyClient, log: Log | Console57 ): StructuredToolInterface[] {58 // Tools are initialized to be passed to the agent59 const airbnbSearch = new AirbnbSearch({ apifyClient, log });60 return [61 airbnbSearch,62 ];63 }64
65 protected buildPrompt(): ChatPromptTemplate {66 const today = new Date().toISOString().slice(0, 10);67 return ChatPromptTemplate.fromMessages([68 ['system',69 'You are a experienced travel agent that knows that Airbnb is a website where you can search for places to stay based on criteria like location, dates and price. '70 + 'Using the recommended locations, search on Airbnb for places that match the user price, date and travelers (adults, children and pets) requirements. '71 + 'The user must specify who is traveling (adults, children, infants, pets) (default to 2 adults if the information is not given). '72 + `When managing dates, you always try to use the best format in the world, which is: yyy-mm-dd. For any calculation assume that today is ${today}.`73 + 'The user can specify if the travel has a start date (default to a trip of one week if the information is not given and choose today if no start date is available). '74 + 'The user can specify if the travel has an end date (default to a trip of one week if the information is not given and choose today if no start date is available). '75 + 'The user can specify if the travel destination has a minimum value (default to 1 if the information is not given). '76 + 'The user can specify if the travel destination has a maximum value (default to 250 if the information is not given). '77 + 'The user can specify a minimum number of baths, beds or bedrooms (omit these search parameters if the information is not given). '78 + 'Inform the user of the assumptions you made to make the Airbnb search.'79 + 'Inform the user the datasetId and the amount of items you received from the airbnb_search tool in order to explore the results later. '80 + "As your response, return only a JSON object (and nothing more! not even a ```json or ``` wrapper) with the keys 'datasetId' and 'assumptions', with their corresponding values in string format, along with the key 'totalItems' with the number of items in the dataset in number format."81 ],82 ['placeholder', '{chat_history}'],83 ['human', '{input}'],84 ['placeholder', '{agent_scratchpad}'],85 ]);86 }87}88
89export default ResearcherAgent;
src/agents/success.ts
1import { Log, ApifyClient } from 'apify';2import { ChatPromptTemplate } from '@langchain/core/prompts';3import { createToolCallingAgent, AgentExecutor } from 'langchain/agents';4import { ChatOpenAI } from '@langchain/openai';5import { StructuredToolInterface } from '@langchain/core/tools';6import { CostHandler } from '../utils/cost_handler.js';7
8/**9 * Interface for parameters required by SuccessAgent class.10 */11export interface SuccessAgentParams {12 apifyClient: ApifyClient,13 modelName: string,14 openaiApiKey: string,15 log: Log | Console;16}17
18/**19 * An AI Agent summarizes the efforts of all agents to answer the users's question.20 */21export class SuccessAgent {22 protected log: Log | Console;23 protected apifyClient: ApifyClient;24 public agentExecutor: AgentExecutor;25 public costHandler: CostHandler;26
27 constructor(fields?: SuccessAgentParams) {28 this.log = fields?.log ?? console;29 this.apifyClient = fields?.apifyClient ?? new ApifyClient();30 this.costHandler = new CostHandler(fields?.modelName ?? 'gpt-4o-mini', this.log);31 const llm = new ChatOpenAI({32 model: fields?.modelName,33 apiKey: fields?.openaiApiKey,34 temperature: 0,35 callbacks: [36 this.costHandler,37 ],38 });39 const tools = this.buildTools();40 const prompt = this.buildPrompt();41 const agent = createToolCallingAgent({42 llm,43 tools,44 prompt,45 });46 this.agentExecutor = new AgentExecutor({47 agent,48 tools,49 verbose: false,50 maxIterations: 3,51 });52 }53
54 protected buildTools(): StructuredToolInterface[] {55 // Tools are initialized to be passed to the agent56 return [];57 }58
59 protected buildPrompt(): ChatPromptTemplate {60 return ChatPromptTemplate.fromMessages([61 ['system',62 'You are a experienced travel agent that wants to help the user plan the perfect trip. '63 + 'You asked some colleagues for help and they sent you a bunch of info to help the user.'64 + 'The user may have specified other requests like pets, bedrooms, beds, baths, gym, etc so make sure to mention them. '65 + 'Make sure that you show the rating, price, image, description, amenities (or other useful information) and a link to view more information about the chosen results. '66 + 'If more than one location was specified in the search, try to make it so that your recommendations include results in every one of them. '67 + 'Please explain why you chose those results and how many you explored before making your choice. '68 ],69 ['placeholder', '{chat_history}'],70 ['human', '{input}'],71 ['placeholder', '{agent_scratchpad}'],72 ]);73 }74}75
76export default SuccessAgent;
src/tools/airbnb_search.ts
1import { Log, ApifyClient } from 'apify';2import { createHash } from 'crypto';3import { StructuredTool } from '@langchain/core/tools';4import { z } from 'zod';5import { chargeForToolUsage } from '../utils/ppe_handler.js';6
7/**8 * Interface for parameters required by AirbnbSearch class.9 */10export interface AirbnbSearchParams {11 apifyClient: ApifyClient;12 log: Log | Console;13}14
15/**16 * Tool that uses the AirbnbSearch function17 */18export class AirbnbSearch extends StructuredTool {19 protected log: Log | Console;20 protected apifyClient: ApifyClient;21
22 name = 'airbnb_search';23
24 description = 'Searches for properties on Airbnb based on a list of Zip Codes (at least one) and returns an Apify datasetId to explore the results.';25
26 schema = z.object({27 locations: z.string().array(),28 minimumPrice: z.number(),29 maximumPrice: z.number(),30 adults: z.number(),31 children: z.number(),32 infants: z.number(),33 pets: z.number().optional(),34 checkIn: z.string().describe('yyyy-mm-dd'),35 checkOut: z.string().describe('yyyy-mm-dd'),36 minBathrooms: z.number().optional(),37 minBedrooms: z.number().optional(),38 minBeds: z.number().optional(),39 });40
41 constructor(fields?: AirbnbSearchParams) {42 super(...arguments);43 this.log = fields?.log ?? console;44 this.apifyClient = fields?.apifyClient ?? new ApifyClient();45 }46
47 override async _call(arg: z.output<typeof this.schema>) {48 const actorInput = {49 adults: arg.adults,50 children: arg.children,51 infants: arg.infants,52 pets: arg.pets,53 checkIn: arg.checkIn,54 checkOut: arg.checkOut,55 minBathrooms: arg.minBathrooms,56 minBedrooms: arg.minBedrooms,57 minBeds: arg.minBeds,58 locationQueries: arg.locations.slice(0, 3),59 priceMax: arg.maximumPrice,60 priceMin: arg.minimumPrice,61 currency: 'USD',62 locale: 'en-US',63 };64 // checks for cached stored version65 const key = JSON.stringify(actorInput);66 const algorithm = 'sha256';67 const digest = createHash(algorithm).update(key).digest('hex').slice(0, 16);68 const { username } = await this.apifyClient.user().get();69 const today = new Date().toISOString().slice(0, 10);70 const datasetName = `${today}-${digest}`;71 // const datasetName = '2025-03-11-37d909c75952bc93'; //DEBUG72 this.log.debug(`Searching for datasetId: ${username}/${datasetName}`);73 const existingDataset = await this.apifyClient74 .dataset(`${username}/${datasetName}`)75 .get();76 this.log.debug(`Found existingDataset? ${existingDataset}`);77 let totalItems = existingDataset?.itemCount;78 if (existingDataset) {79 this.log.debug(80 `Cached response found for: ${JSON.stringify(actorInput)}`81 );82 } else {83 this.log.debug(84 `Calling AirbnbSearch with input: ${JSON.stringify(actorInput)}`85 );86 const actorRun = await this.apifyClient87 .actor('tri_angle/new-fast-airbnb-scraper')88 .call(actorInput, { maxItems: 100 }); // DEBUG89 await this.apifyClient90 .dataset(actorRun.defaultDatasetId)91 .update({ name: datasetName });92 const dataset = await this.apifyClient93 .dataset(actorRun.defaultDatasetId)94 .listItems();95 totalItems = dataset.total;96 await chargeForToolUsage(this.name, dataset.total);97 }98 this.log.debug(`AirbnbSearch response: ${username}/${datasetName}`);99 return `Results for Airbnb Search can be found in dataset with id '${username}/${datasetName}'. This dataset contains ${totalItems} items in total.`;100 }101}102
103export default AirbnbSearch;
src/tools/dataset_explorer.ts
1import { Log, ApifyClient } from 'apify';2import { StructuredTool } from '@langchain/core/tools';3import { z } from 'zod';4
5/**6 * Interface for parameters required by DatasetExplorer class.7 */8export interface DatasetExplorerParams {9 apifyClient: ApifyClient;10 log: Log | Console;11}12
13/**14 * Allows the LLM to explore a paginated set. Useful if the dataset is too big.15 */16export class DatasetExplorer extends StructuredTool {17 protected log: Log | Console;18 protected apifyClient: ApifyClient;19
20 name = 'dataset_explorer';21
22 description = 'Retrieves paginated data from an Apify dataset. If no "limit" is specified, it returns only 10 results. Increate "offset" to return other results and make sure that "offset" + "limit" does not exceed the "total" of items.';23
24 schema = z.object({25 datasetId: z.string(),26 fields: z.string().array(),27 offset: z.number() || 0,28 limit: z.number() || 10,29 });30
31 constructor(fields?: DatasetExplorerParams) {32 super(...arguments);33 this.log = fields?.log ?? console;34 this.apifyClient = fields?.apifyClient ?? new ApifyClient();35 }36
37 override async _call(arg: z.output<typeof this.schema>) {38 this.log.debug(39 `Calling DatasetExplorer with input: ${JSON.stringify(arg)}`40 );41 const dataset = await this.apifyClient.dataset(arg.datasetId).get();42 if (!dataset) return 'Dataset not found.';43 const datasetItems = await this.apifyClient44 .dataset(arg.datasetId)45 .listItems({46 clean: true,47 fields: arg.fields,48 offset: arg.offset,49 limit: arg.limit,50 });51 this.log.debug(52 `DatasetExplorer response: ${JSON.stringify(datasetItems).slice(0, 100)}...`53 );54 return `The data for dataset ${arg.datasetId} with offset ${arg.offset} and limit ${arg.limit} is as follows: ${JSON.stringify(datasetItems)}`;55 }56}57
58export default DatasetExplorer;
src/tools/duck_duck_go.ts
1import { Log, ApifyClient } from 'apify';2import { createHash } from 'crypto';3import { StructuredTool } from '@langchain/core/tools';4import { z } from 'zod';5import { chargeForToolUsage } from '../utils/ppe_handler.js';6
7/**8 * Interface for parameters required by DuckDuckGo class.9 */10export interface DuckDuckGoParams {11 apifyClient: ApifyClient;12 log: Log | Console;13}14
15/**16 * Tool that uses the DuckDuckGo function17 */18export class DuckDuckGo extends StructuredTool {19 protected log: Log | Console;20 protected apifyClient: ApifyClient;21
22 name = 'duck_duck_go';23
24 description = 'Performs a search on the search engine DuckDuckGo and returns a stringified JSON with the results.';25
26 schema = z.object({27 keywords: z.string(),28 locale: z.enum(['us-en', 'uk-en', 'cz-cs', 'cl-es']) || undefined,29 maximum: z.number() || undefined,30 });31
32 constructor(fields?: DuckDuckGoParams) {33 super(...arguments);34 this.log = fields?.log ?? console;35 this.apifyClient = fields?.apifyClient ?? new ApifyClient();36 }37
38 override async _call(arg: z.output<typeof this.schema>) {39 const actorInput = {40 keywords: arg.keywords,41 proxy: {42 useApifyProxy: true,43 apifyProxyGroups: [44 'RESIDENTIAL'45 ],46 apifyProxyCountry: 'US'47 },48 locale: arg.locale ?? 'us-en',49 operation: 'st',50 safe: 'Partial',51 daterange: 'all',52 img_type: 'all',53 img_size: 'all',54 vid_duration: 'all',55 maximum: arg.maximum ?? 5,56 timeout: 3057 };58 // checks for cached stored version59 const key = JSON.stringify(actorInput);60 const algorithm = 'sha256';61 const digest = createHash(algorithm).update(key).digest('hex').slice(0, 16);62 const { username } = await this.apifyClient.user().get();63 const today = new Date().toISOString().slice(0, 10);64 const datasetName = `${today}-${digest}`;65 this.log.debug(`Searching for datasetId: ${username}/${datasetName}`);66 const existingDataset = await this.apifyClient67 .dataset(`${username}/${datasetName}`)68 .get();69 this.log.debug(`Found existingDataset? ${existingDataset}`);70 let dataset;71 if (existingDataset) {72 this.log.debug(73 `Cached response found for: ${JSON.stringify(actorInput)}`74 );75 dataset = await this.apifyClient76 .dataset(`${username}/${datasetName}`)77 .listItems();78 } else {79 this.log.debug(80 `Calling DuckDuckGo with input: ${JSON.stringify(actorInput)}`81 );82 const actorRun = await this.apifyClient83 .actor('canadesk/duckduckgo-serp-api')84 .call(actorInput);85 dataset = await this.apifyClient86 .dataset(actorRun.defaultDatasetId)87 .listItems();88 await this.apifyClient89 .dataset(actorRun.defaultDatasetId)90 .update({ name: datasetName });91 await chargeForToolUsage(this.name, 1);92 }93 const { items } = dataset;94 this.log.debug(`DuckDuckGo response: ${JSON.stringify(items)}`);95 return JSON.stringify(items);96 }97}98
99export default DuckDuckGo;
src/tools/magictool.ts
1import { Log } from 'apify';2import { Tool } from '@langchain/core/tools';3
4/**5 * Interface for parameters required by MagicTool class.6 */7export interface MagicToolParams {8 apiKey?: string;9 log: Log | Console;10}11
12/**13 * Tool that uses the MagicTool function. This is an example function to use as template.14 */15export class MagicTool extends Tool {16 static override lc_name() {17 return 'MagicTool';18 }19
20 protected log: Log | Console;21 protected apiKey: string;22
23 name = 'magic_function';24
25 description = 'Applies a magic function to an input.';26
27 constructor(fields?: MagicToolParams) {28 super(...arguments);29 this.log = fields?.log ?? console;30 const apiKey = fields?.apiKey ?? process.env.SECRET_API_KEY ?? '';31 if (apiKey === undefined) {32 this.log.debug(33 "Secret API key not set. You can set it as 'SECRET_API_KEY' in your environment variables."34 );35 }36 this.apiKey = apiKey;37 }38
39 async _call(rawInput: string) {40 this.log.debug(`rawInput: ${rawInput}`);41 const number = parseInt(rawInput, 10);42 return `${number + 2}`;43 }44}45
46export default MagicTool;
src/tools/website_scraper.ts
1import { Log, ApifyClient } from 'apify';2import { createHash } from 'crypto';3import { StructuredTool } from '@langchain/core/tools';4import { z } from 'zod';5import { chargeForToolUsage } from '../utils/ppe_handler.js';6
7/**8 * Interface for parameters required by WebsiteScraper class.9 */10export interface WebsiteScraperParams {11 apifyClient: ApifyClient;12 log: Log | Console;13}14
15/**16 * An example input that serves as a good default17 */18const sampleInput = {19 method: 'getAllItems',20 output: '{"results":[""]}',21};22
23/**24 * Tool that uses the WebsiteScraper function25 */26export class WebsiteScraper extends StructuredTool {27 protected log: Log | Console;28 protected apifyClient: ApifyClient;29
30 name = 'website_scraper';31
32 description = 'Scrapes a URL and then performs a search based on a specified method (for example: getAllItems) and returns a stringified JSON with the results using the default output format (for example: {"results":[""]}).';33
34 schema = z.object({35 url: z.string(),36 method: z.string() || undefined,37 // output: z.string() || undefined || null,38 });39
40 constructor(fields?: WebsiteScraperParams) {41 super(...arguments);42 this.log = fields?.log ?? console;43 this.apifyClient = fields?.apifyClient ?? new ApifyClient();44 }45
46 override async _call(arg: z.output<typeof this.schema>) {47 const actorInput = {48 url: arg.url,49 method: arg.method ?? sampleInput.method,50 output: sampleInput.output,51 };52 // checks for cached stored version53 const key = JSON.stringify(actorInput);54 const algorithm = 'sha256';55 const digest = createHash(algorithm).update(key).digest('hex').slice(0, 16);56 const { username } = await this.apifyClient.user().get();57 const thisMonth = new Date().toISOString().slice(0, 7);58 const datasetName = `${thisMonth}-${digest}`;59 this.log.debug(`Searching for datasetId: ${username}/${datasetName}`);60 const existingDataset = await this.apifyClient61 .dataset(`${username}/${datasetName}`)62 .get();63 this.log.debug(`Found existingDataset? ${existingDataset}`);64 let dataset;65 if (existingDataset) {66 this.log.debug(67 `Cached response found for: ${JSON.stringify(actorInput)}`68 );69 dataset = await this.apifyClient70 .dataset(`${username}/${datasetName}`)71 .listItems();72 } else {73 const callOptions = {74 timeout: 60,75 };76 this.log.debug(77 `Calling WebsiteScraper with input: ${JSON.stringify(actorInput)}`78 );79 const actorRun = await this.apifyClient80 .actor('zeeb0t/web-scraping-api---scrape-any-website')81 .call(actorInput, callOptions);82 dataset = await this.apifyClient83 .dataset(actorRun.defaultDatasetId)84 .listItems();85 if (dataset.total > 0) {86 await this.apifyClient87 .dataset(actorRun.defaultDatasetId)88 .update({ name: datasetName });89 }90 await chargeForToolUsage(this.name, 1);91 }92 const { items } = dataset;93 this.log.debug(`WebsiteScraper response: ${JSON.stringify(items)}`);94 return `Scraped ${arg.url}, ran the method ${arg.method} on the content and obtained: ${JSON.stringify(items)}`;95 }96}97
98export default WebsiteScraper;
src/utils/cost_handler.ts
1import { Log } from 'apify';2import { BaseTracer } from 'langchain/callbacks';3import { Run } from '@langchain/core/tracers/tracer_langchain';4import { GPT_MODEL_LIST, OpenaiAPICost } from './openai_models.js';5import { chargeForModelTokens } from './ppe_handler.js';6
7interface TotalCost {8 usd: number;9 inputTokens: number;10 outputTokens: number;11 totalModelCalls: number;12}13
14export class CostHandler extends BaseTracer {15 protected log: Log | Console;16 name: string;17 modelName: string;18 modelCost: OpenaiAPICost;19 totalCost: TotalCost;20
21 constructor(modelName: string, log?: Log | Console) {22 super();23 this.log = log ?? console;24 this.name = 'cost_handler';25 this.modelName = modelName;26 this.modelCost = GPT_MODEL_LIST[this.modelName].cost;27 this.totalCost = {28 usd: 0,29 inputTokens: 0,30 outputTokens: 0,31 totalModelCalls: 0,32 };33 }34
35 // NOTE: We do not need to persist runs in this handler.36 // eslint-disable-next-line @typescript-eslint/no-unused-vars37 persistRun(_run: Run) {38 return Promise.resolve();39 }40
41 /**42 * Logs tokens usage in $.43 * @returns void44 */45 override onLLMEnd(run: Run) {46 const tokenUsage = run?.outputs?.llmOutput?.tokenUsage;47 if (tokenUsage) {48 const inputCostsUSD = this.modelCost.input49 * (tokenUsage.promptTokens / 1000);50 const outputCostsUSD = this.modelCost.output51 * (tokenUsage.completionTokens / 1000);52 const callCostUSD = inputCostsUSD + outputCostsUSD;53 this.totalCost.usd += inputCostsUSD + outputCostsUSD;54 this.totalCost.inputTokens += tokenUsage.promptTokens;55 this.totalCost.outputTokens += tokenUsage.completionTokens;56 this.totalCost.totalModelCalls++;57 const durationSecs = run.end_time && run.start_time58 && (run.end_time - run.start_time) / 1000;59 this.log.debug(`LLM model call processed`,60 {61 durationSecs,62 callCostUSD,63 totalCostUSD: this.totalCost.usd,64 inputTokens: this.totalCost.inputTokens,65 outputTokens: this.totalCost.outputTokens,66 }67 );68 }69 }70
71 override onLLMStart(run: Run) {72 this.log.debug(`Calling LLM model`, run);73 }74
75 getTotalCost() {76 return this.totalCost;77 }78
79 async logOrChargeForTokens(modelName: string, tokenCostActive: boolean) {80 const costs = this.totalCost;81 if (tokenCostActive) {82 const tokens = costs.inputTokens + costs.outputTokens;83 const tokensCost = this.modelCost.output * (tokens / 1000);84 this.log.info(`Total tokens processed: ${tokens}. Usage cost: ${tokensCost}`);85 await chargeForModelTokens(modelName, tokens);86 } else {87 this.log.info(`Estimated OpenAI cost: $${costs.usd} USD`);88 }89 }90}
src/utils/openai_models.ts
1export interface OpenaiAPICost {2 input: number; // USD cost per 1000 tokens3 output: number; // USD cost per 1000 tokens4}5
6export interface GPTModelConfig {7 model: string;8 maxTokens: number;9 maxOutputTokens?: number;10 interface: 'text' | 'chat';11 cost: OpenaiAPICost; // USD cost per 1000 tokens12}13
14/**15* List of GPT models that can be used.16* Should be in sync with https://platform.openai.com/docs/models/17* Last updated on 2025-03-0918*/19export const GPT_MODEL_LIST: {[key: string]: GPTModelConfig} = {20 'gpt-4o': {21 model: 'gpt-4o',22 maxTokens: 16384,23 interface: 'chat',24 cost: {25 input: 0.0025,26 output: 0.01,27 },28 },29 'gpt-4o-mini': {30 model: 'gpt-4o-mini',31 maxTokens: 16384,32 interface: 'chat',33 cost: {34 input: 0.00015,35 output: 0.0006,36 },37 },38 'gpt-3.5-turbo': {39 model: 'gpt-3.5-turbo',40 maxTokens: 8192,41 interface: 'chat',42 cost: {43 input: 0.0005,44 output: 0.0015,45 },46 }47};
src/utils/ppe_events.ts
1export const PPE_EVENT = {2 ACTOR_START_GB: 'actor-start-gb',3 GPT_4O: 'openai-1000-tokens-gpt-4o',4 GPT_4O_MINI: 'openai-1000-tokens-gpt-4o-mini',5 GPT_3_5_TURBO: 'openai-1000-tokens-gpt-3-5-turbo',6 DUCK_DUCK_GO: 'duck-duck-go',7 WEBSITE_SCRAPER: 'website-scraper',8 AIRBNB_SEARCH: 'airbnb-search',9 TRIPADVISOR_SEARCH: 'tripadvisor-search',10} as const;
src/utils/ppe_handler.ts
1import { Actor, log } from 'apify';2import { PPE_EVENT } from './ppe_events.js';3
4/**5 * Charges for the tokens used by a specific model.6 *7 * @param modelName - The name of the model.8 * @param tokens - The number of tokens to charge for.9 * @throws Will throw an error if the model name is unknown.10 */11export async function chargeForModelTokens(modelName: string, tokens: number) {12 const tokensK = Math.ceil(tokens / 1000);13 log.debug(`Charging for ${tokens} tokens (${tokensK}k) for model ${modelName}`);14 let eventName: string = PPE_EVENT.GPT_4O;15 switch (modelName) {16 case 'gpt-4o-mini':17 eventName = PPE_EVENT.GPT_4O_MINI;18 break;19 case 'gpt-3.5-turbo':20 eventName = PPE_EVENT.GPT_3_5_TURBO;21 break;22 default:23 eventName = PPE_EVENT.GPT_4O;24 break;25 }26 await Actor.charge(27 { eventName, count: tokensK }28 );29}30
31export async function chargeForActorStart() {32 if (33 Actor.getChargingManager()34 .getChargedEventCount(PPE_EVENT.ACTOR_START_GB) === 035 ) {36 const count = Math.ceil((Actor.getEnv().memoryMbytes || 1024) / 1024);37 await Actor.charge({ eventName: PPE_EVENT.ACTOR_START_GB, count });38 }39}40
41export async function chargeForToolUsage(toolName: string, count: number) {42 log.debug(`Charging #${count} times for tool ${toolName}`);43 let eventName: string = '';44 switch (toolName) {45 case 'duck_duck_go':46 eventName = PPE_EVENT.DUCK_DUCK_GO;47 break;48 case 'website_scraper':49 eventName = PPE_EVENT.WEBSITE_SCRAPER;50 break;51 case 'airbnb_search':52 eventName = PPE_EVENT.AIRBNB_SEARCH;53 break;54 default:55 eventName = '';56 break;57 }58 await Actor.charge({ eventName, count });59}