AI Travel Agent avatar
AI Travel Agent

Pricing

Pay per event

Go to Store
AI Travel Agent

AI Travel Agent

maxfindel/ai-travel-agent

Developed by

Max F. Findel

Maintained by Community

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

Monthly users

2

Runs succeeded

91%

Last modified

4 days ago

.dockerignore

1# configurations
2.idea
3.vscode
4
5# crawlee and apify storage folders
6apify_storage
7crawlee_storage
8storage
9
10# installed files
11node_modules
12
13# git folder
14.git
15
16# dist folder
17dist

.editorconfig

1root = true
2
3[*]
4indent_style = space
5indent_size = 2
6charset = utf-8
7trim_trailing_whitespace = true
8insert_final_newline = true
9end_of_line = lf

.env.example

1USER_APIFY_TOKEN="apify_api_1234"
2ACTOR_TEST_PAY_PER_EVENT=true
3LANGSMITH_TRACING=false
4LANGSMITH_ENDPOINT="https://api.smith.langchain.com"
5LANGSMITH_API_KEY="lsv2_pt_1234"
6LANGSMITH_PROJECT="langsmith-cool-project-name"
7OPENAI_API_KEY="sk-proj-1234"

.eslintrc

1{
2  "root": false,
3  "env": {
4    "browser": true,
5    "es2020": true,
6    "node": true
7  },
8  "extends": [
9    "@apify/eslint-config-ts"
10  ],
11  "parserOptions": {
12    "project": "./tsconfig.json",
13    "ecmaVersion": 2020
14  },
15  "ignorePatterns": [
16    "node_modules",
17    "dist",
18    "**/*.d.ts"
19  ],
20  "rules": {
21    "indent": ["error", 2],
22    "comma-dangle": ["off", 0],
23    "prefer-rest-params": ["off", 0],
24    "max-len": ["error", { "ignoreComments": true, "ignoreStrings": true, "ignoreUrls": true, "ignoreTemplateLiterals": true }]
25  }
26}

.gitignore

1# This file tells Git which files shouldn't be added to source control
2
3.idea
4.vscode
5storage
6apify_storage
7crawlee_storage
8node_modules
9dist
10tsconfig.tsbuildinfo
11.env
12
13# Added by Apify CLI
14.venv

.gitpod.yml

1image: gitpod/workspace-full:latest
2tasks:
3  - name: main
4    init: >
5      nvm use lts/jod &&
6      npm install -g npm@11 &&
7      npm install -g eslint@^8.50.0 &&
8      npm install -g apify-cli
9    command: echo "Login to apify to get an API Key, export your APIFY_TOKEN, create an INPUT.json file and run npm start!"
10  - name: config
11    before: >
12      (([[ ! -z $GITCONFIG ]] &&
13      echo $GITCONFIG | base64 -d > ~/.gitconfig &&
14      chmod 644 ~/.gitconfig) || unset GITCONFIG) &&
15      (([[ ! -z $GNUPG_1 ]] &&
16      rm -rf ~/.gnupg &&
17      cd / &&
18      echo $GNUPG_1$GNUPG_2 | base64 -d | tar --no-same-owner -xzf -) || unset GNUPG_1)
19    command: echo "Ready! Start a new terminal session in order to use GPG." && exit

LICENSE

1Apache License
2                           Version 2.0, January 2004
3                        http://www.apache.org/licenses/
4
5   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
7   1. Definitions.
8
9      "License" shall mean the terms and conditions for use, reproduction,
10      and distribution as defined by Sections 1 through 9 of this document.
11
12      "Licensor" shall mean the copyright owner or entity authorized by
13      the copyright owner that is granting the License.
14
15      "Legal Entity" shall mean the union of the acting entity and all
16      other entities that control, are controlled by, or are under common
17      control with that entity. For the purposes of this definition,
18      "control" means (i) the power, direct or indirect, to cause the
19      direction or management of such entity, whether by contract or
20      otherwise, or (ii) ownership of fifty percent (50%) or more of the
21      outstanding shares, or (iii) beneficial ownership of such entity.
22
23      "You" (or "Your") shall mean an individual or Legal Entity
24      exercising permissions granted by this License.
25
26      "Source" form shall mean the preferred form for making modifications,
27      including but not limited to software source code, documentation
28      source, and configuration files.
29
30      "Object" form shall mean any form resulting from mechanical
31      transformation or translation of a Source form, including but
32      not limited to compiled object code, generated documentation,
33      and conversions to other media types.
34
35      "Work" shall mean the work of authorship, whether in Source or
36      Object form, made available under the License, as indicated by a
37      copyright notice that is included in or attached to the work
38      (an example is provided in the Appendix below).
39
40      "Derivative Works" shall mean any work, whether in Source or Object
41      form, that is based on (or derived from) the Work and for which the
42      editorial revisions, annotations, elaborations, or other modifications
43      represent, as a whole, an original work of authorship. For the purposes
44      of this License, Derivative Works shall not include works that remain
45      separable from, or merely link (or bind by name) to the interfaces of,
46      the Work and Derivative Works thereof.
47
48      "Contribution" shall mean any work of authorship, including
49      the original version of the Work and any modifications or additions
50      to that Work or Derivative Works thereof, that is intentionally
51      submitted to Licensor for inclusion in the Work by the copyright owner
52      or by an individual or Legal Entity authorized to submit on behalf of
53      the copyright owner. For the purposes of this definition, "submitted"
54      means any form of electronic, verbal, or written communication sent
55      to the Licensor or its representatives, including but not limited to
56      communication on electronic mailing lists, source code control systems,
57      and issue tracking systems that are managed by, or on behalf of, the
58      Licensor for the purpose of discussing and improving the Work, but
59      excluding communication that is conspicuously marked or otherwise
60      designated in writing by the copyright owner as "Not a Contribution."
61
62      "Contributor" shall mean Licensor and any individual or Legal Entity
63      on behalf of whom a Contribution has been received by Licensor and
64      subsequently incorporated within the Work.
65
66   2. Grant of Copyright License. Subject to the terms and conditions of
67      this License, each Contributor hereby grants to You a perpetual,
68      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69      copyright license to reproduce, prepare Derivative Works of,
70      publicly display, publicly perform, sublicense, and distribute the
71      Work and such Derivative Works in Source or Object form.
72
73   3. Grant of Patent License. Subject to the terms and conditions of
74      this License, each Contributor hereby grants to You a perpetual,
75      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76      (except as stated in this section) patent license to make, have made,
77      use, offer to sell, sell, import, and otherwise transfer the Work,
78      where such license applies only to those patent claims licensable
79      by such Contributor that are necessarily infringed by their
80      Contribution(s) alone or by combination of their Contribution(s)
81      with the Work to which such Contribution(s) was submitted. If You
82      institute patent litigation against any entity (including a
83      cross-claim or counterclaim in a lawsuit) alleging that the Work
84      or a Contribution incorporated within the Work constitutes direct
85      or contributory patent infringement, then any patent licenses
86      granted to You under this License for that Work shall terminate
87      as of the date such litigation is filed.
88
89   4. Redistribution. You may reproduce and distribute copies of the
90      Work or Derivative Works thereof in any medium, with or without
91      modifications, and in Source or Object form, provided that You
92      meet the following conditions:
93
94      (a) You must give any other recipients of the Work or
95          Derivative Works a copy of this License; and
96
97      (b) You must cause any modified files to carry prominent notices
98          stating that You changed the files; and
99
100      (c) You must retain, in the Source form of any Derivative Works
101          that You distribute, all copyright, patent, trademark, and
102          attribution notices from the Source form of the Work,
103          excluding those notices that do not pertain to any part of
104          the Derivative Works; and
105
106      (d) If the Work includes a "NOTICE" text file as part of its
107          distribution, then any Derivative Works that You distribute must
108          include a readable copy of the attribution notices contained
109          within such NOTICE file, excluding those notices that do not
110          pertain to any part of the Derivative Works, in at least one
111          of the following places: within a NOTICE text file distributed
112          as part of the Derivative Works; within the Source form or
113          documentation, if provided along with the Derivative Works; or,
114          within a display generated by the Derivative Works, if and
115          wherever such third-party notices normally appear. The contents
116          of the NOTICE file are for informational purposes only and
117          do not modify the License. You may add Your own attribution
118          notices within Derivative Works that You distribute, alongside
119          or as an addendum to the NOTICE text from the Work, provided
120          that such additional attribution notices cannot be construed
121          as modifying the License.
122
123      You may add Your own copyright statement to Your modifications and
124      may provide additional or different license terms and conditions
125      for use, reproduction, or distribution of Your modifications, or
126      for any such Derivative Works as a whole, provided Your use,
127      reproduction, and distribution of the Work otherwise complies with
128      the conditions stated in this License.
129
130   5. Submission of Contributions. Unless You explicitly state otherwise,
131      any Contribution intentionally submitted for inclusion in the Work
132      by You to the Licensor shall be under the terms and conditions of
133      this License, without any additional terms or conditions.
134      Notwithstanding the above, nothing herein shall supersede or modify
135      the terms of any separate license agreement you may have executed
136      with Licensor regarding such Contributions.
137
138   6. Trademarks. This License does not grant permission to use the trade
139      names, trademarks, service marks, or product names of the Licensor,
140      except as required for reasonable and customary use in describing the
141      origin of the Work and reproducing the content of the NOTICE file.
142
143   7. Disclaimer of Warranty. Unless required by applicable law or
144      agreed to in writing, Licensor provides the Work (and each
145      Contributor provides its Contributions) on an "AS IS" BASIS,
146      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147      implied, including, without limitation, any warranties or conditions
148      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149      PARTICULAR PURPOSE. You are solely responsible for determining the
150      appropriateness of using or redistributing the Work and assume any
151      risks associated with Your exercise of permissions under this License.
152
153   8. Limitation of Liability. In no event and under no legal theory,
154      whether in tort (including negligence), contract, or otherwise,
155      unless required by applicable law (such as deliberate and grossly
156      negligent acts) or agreed to in writing, shall any Contributor be
157      liable to You for damages, including any direct, indirect, special,
158      incidental, or consequential damages of any character arising as a
159      result of this License or out of the use or inability to use the
160      Work (including but not limited to damages for loss of goodwill,
161      work stoppage, computer failure or malfunction, or any and all
162      other commercial damages or losses), even if such Contributor
163      has been advised of the possibility of such damages.
164
165   9. Accepting Warranty or Additional Liability. While redistributing
166      the Work or Derivative Works thereof, You may choose to offer,
167      and charge a fee for, acceptance of support, warranty, indemnity,
168      or other liability obligations and/or rights consistent with this
169      License. However, in accepting such obligations, You may act only
170      on Your own behalf and on Your sole responsibility, not on behalf
171      of any other Contributor, and only if You agree to indemnify,
172      defend, and hold each Contributor harmless for any liability
173      incurred by, or claims asserted against, such Contributor by reason
174      of your accepting any such warranty or additional liability.
175
176   END OF TERMS AND CONDITIONS
177
178   APPENDIX: How to apply the Apache License to your work.
179
180      To apply the Apache License to your work, attach the following
181      boilerplate notice, with the fields enclosed by brackets "[]"
182      replaced with your own identifying information. (Don't include
183      the brackets!)  The text should be enclosed in the appropriate
184      comment syntax for the file format. We also recommend that a
185      file or class name and description of purpose be included on the
186      same "printed page" as the copyright notice for easier
187      identification within third-party archives.
188
189   Copyright [yyyy] [name of copyright owner]
190
191   Licensed under the Apache License, Version 2.0 (the "License");
192   you may not use this file except in compliance with the License.
193   You may obtain a copy of the License at
194
195       http://www.apache.org/licenses/LICENSE-2.0
196
197   Unless required by applicable law or agreed to in writing, software
198   distributed under the License is distributed on an "AS IS" BASIS,
199   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200   See the License for the specific language governing permissions and
201   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

1{
2  "name": "ai-travel-agent",
3  "version": "0.0.1",
4  "type": "module",
5  "description": "AI Agent Actor that leverages Apify to search for travel opportunities.",
6  "engines": {
7    "node": ">=22.0.0"
8  },
9  "dependencies": {
10    "@langchain/core": "^0.3.42",
11    "@langchain/langgraph": "^0.2.54",
12    "@langchain/openai": "^0.4.4",
13    "apify": "^3.3.2",
14    "crypto": "^1.0.1",
15    "dotenv": "^16.4.7",
16    "langchain": "^0.3.19",
17    "zod": "^3.24.2"
18  },
19  "devDependencies": {
20    "@apify/eslint-config-ts": "^0.3.0",
21    "@apify/tsconfig": "^0.1.0",
22    "@eslint/js": "^9.21.0",
23    "@types/node": "^22.0.0",
24    "@typescript-eslint/eslint-plugin": "^7.18.0",
25    "@typescript-eslint/parser": "^7.18.0",
26    "eslint": "^8.57.1",
27    "globals": "^16.0.0",
28    "tsx": "^4.4.0",
29    "typescript": "~5.5.0",
30    "typescript-eslint": "^8.26.0"
31  },
32  "scripts": {
33    "start": "npm run start:dev",
34    "start:prod": "node dist/main.js",
35    "start:dev": "tsx src/main.ts",
36    "build": "tsc",
37    "lint": "eslint src",
38    "test": "echo \"Error: oops, the actor has no tests yet, sad!\" && exit 1"
39  },
40  "author": "It's not you it's me",
41  "license": "ISC"
42}

tsconfig.json

1{
2  "extends": "@apify/tsconfig",
3  "compilerOptions": {
4    "module": "NodeNext",
5    "moduleResolution": "NodeNext",
6    "target": "ES2022",
7    "outDir": "dist",
8    "noUnusedLocals": false,
9    "skipLibCheck": true,
10    "lib": ["DOM"],
11  },
12  "include": [
13    "./*.d.ts",
14    "./src/**/*",
15  ]
16}

.actor/Dockerfile

1# Specify the base Docker image. You can read more about
2# the available images at https://docs.apify.com/sdk/js/docs/guides/docker-images
3# You can also use any other image from Docker Hub.
4FROM apify/actor-node:22 AS builder
5
6# Check preinstalled packages
7RUN npm ls crawlee apify puppeteer playwright
8
9# Copy just package.json and package-lock.json
10# to speed up the build using Docker layer cache.
11COPY package*.json ./
12
13# Install all dependencies. Don't audit to speed up the installation.
14RUN npm install --include=dev --audit=false
15
16# Next, copy the source files using the user set
17# in the base image.
18COPY . ./
19
20# Install all dependencies and build the project.
21# Don't audit to speed up the installation.
22RUN npm run build
23
24# Create final image
25FROM apify/actor-node:22
26
27# Check preinstalled packages
28RUN npm ls crawlee apify puppeteer playwright
29
30# Copy just package.json and package-lock.json
31# to speed up the build using Docker layer cache.
32COPY package*.json ./
33
34# Install NPM packages, skip optional and development dependencies to
35# keep the image small. Avoid logging too much and print the dependency
36# tree for debugging
37RUN npm --quiet set progress=false \
38    && npm install --omit=dev --omit=optional \
39    && echo "Installed NPM packages:" \
40    && (npm list --omit=dev --all || true) \
41    && echo "Node.js version:" \
42    && node --version \
43    && echo "NPM version:" \
44    && npm --version \
45    && rm -r ~/.npm
46
47# Copy built JS files from builder image
48COPY --from=builder /usr/src/app/dist ./dist
49
50# Next, copy the remaining files and directories with the source code.
51# Since we do this after NPM install, quick build will be really fast
52# for most source file changes.
53COPY . ./
54
55# Create and run as a non-root user.
56RUN adduser -h /home/apify -D apify && \
57    chown -R apify:apify ./
58USER apify
59
60# Run the image.
61CMD npm run start:prod --silent

.actor/actor.json

1{
2  "actorSpecification": 1,
3  "name": "ai-travel-agent",
4  "version": "0.1",
5  "buildTag": "latest",
6  "title": "AI Travel Agent",
7  "description": "AI agent that uses a multi-agent approach to find you the travel recommendations using a natural language prompt.",
8  "meta": {
9    "templateId": "ts-empty"
10  },
11  "environmentVariables": {
12    "OPENAI_API_KEY": "@defaultOpenaiApiKey",
13    "USER_APIFY_TOKEN": "@defaultApifyToken"
14  },
15  "dockerfile": "./Dockerfile",
16  "input": "./input_schema.json",
17  "storages": {
18    "dataset": "./dataset_schema.json"
19  }
20}

.actor/dataset_schema.json

1{
2  "actorSpecification": 1,
3  "views": {
4    "overview": {
5      "title": "Overview",
6      "transformation": {
7        "fields": ["actorName", "response"]
8      },
9      "display": {
10        "component": "table",
11        "properties": {
12          "actorName": {
13            "label": "Actor name",
14            "format": "text"
15          },
16          "response": {
17            "label": "Actor evaluation",
18            "format": "text"
19          }
20        }
21      }
22    }
23  }
24}

.actor/input_schema.json

1{
2  "title": "Web Automation Agent",
3  "type": "object",
4  "schemaVersion": 1,
5  "properties": {
6    "instructions": {
7      "title": "Instructions for the AI Travel Agent",
8      "type": "string",
9      "description": "Ask the agent for help to plan the perfect trip.",
10      "editor": "textarea",
11      "prefill": "I want to buy a house with a pool in Miami for less than 1 million dollars. Can you help me?"
12    },
13    "openaiApiKey": {
14      "title": "OpenAI API key",
15      "type": "string",
16      "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>.",
17      "editor": "textfield",
18      "isSecret": true
19    },
20    "model": {
21      "title": "GPT model",
22      "type": "string",
23      "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.",
24      "editor": "select",
25      "default": "gpt-4o-mini",
26      "prefill": "gpt-4o-mini",
27      "enum": ["gpt-4o", "gpt-4o-mini", "gpt-3.5-turbo"]
28    },
29    "debug": {
30      "title": "Debug Mode",
31      "type": "boolean",
32      "description": "Mark this option as true if you want to see all the logs when running the actor.",
33      "editor": "checkbox",
34      "default": false
35    }
36  },
37  "required": ["instructions", "model"]
38}

.actor/pay_per_event.json

1{
2  "actor-start-gb": {
3    "eventTitle": "Actor start per 1 GB",
4    "eventDescription": "Flat fee for starting an Actor run for each 1 GB of memory.",
5    "eventPriceUsd": 0.01
6  },
7  "openai-1000-tokens-gpt-4o": {
8    "eventTitle": "Price per 1000 OpenAI tokens for gpt-4o",
9    "eventDescription": "Flat fee for every 1000 tokens (input/output) used with gpt-4o.",
10    "eventPriceUsd": 0.01
11  },
12  "openai-1000-tokens-gpt-4o-mini": {
13    "eventTitle": "Price per 1000 OpenAI tokens for gpt-4o-mini",
14    "eventDescription": "Flat fee for every 1000 tokens (input/output) used with gpt-4o-mini.",
15    "eventPriceUsd": 0.0006
16  },
17  "openai-1000-tokens-gpt-3-5-turbo": {
18    "eventTitle": "Price per 1000 OpenAI tokens for gpt-3.5-turbo",
19    "eventDescription": "Flat fee for every 1000 tokens (input/output) used with gpt-3.5-turbo.",
20    "eventPriceUsd": 0.0015
21  },
22  "duck-duck-go": {
23    "eventTitle": "Price per Duck Duck Go search",
24    "eventDescription": "Flat fee for every Duck Duck Go search.",
25    "eventPriceUsd": 0.01
26  },
27  "website-scraper':": {
28    "eventTitle": "Price per web page scraped",
29    "eventDescription": "Flat fee for every web page scraped.",
30    "eventPriceUsd": 0.01
31  },
32  "airbnb-search':": {
33    "eventTitle": "Price per Airbnb search result",
34    "eventDescription": "Flat fee for every result when searching Airbnb.",
35    "eventPriceUsd": 0.001
36  },
37  "tripadvisor-search':": {
38    "eventTitle": "Price per TripAdvisor search result",
39    "eventDescription": "Flat fee for every result when searching TripAdvisor.",
40    "eventPriceUsd": 0.003
41  }
42}

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 loaded
11dotenv.config();
12
13/**
14 * Actor input schema
15 */
16type Input = {
17  instructions: string;
18  modelName?: string;
19  openaiApiKey?: string;
20  debug?: boolean;
21}
22
23/**
24 * Actor initialization code and initial charge
25*/
26await Actor.init();
27await chargeForActorStart();
28
29// Handle and validate input
30const {
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 datasets
53const 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 schema
63 */
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    allResultsChecked
184    || thousandResultsChecked
185    || fifteenRecommendationsFound
186  ) {
187    // if any of the above criteria are met, it passes the ball to the successAgent
188    return 'success';
189  }
190  // if none of the above criteria are met, it runs the same node again
191  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-aware
236);
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 | Console
57  ): StructuredToolInterface[] {
58    // Tools are initialized to be passed to the agent
59    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 | Console
58  ): StructuredToolInterface[] {
59    // Tools are initialized to be passed to the agent
60    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 DuckDuckGo
80        + '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 | Console
57  ): StructuredToolInterface[] {
58    // Tools are initialized to be passed to the agent
59    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 agent
56    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 function
17 */
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 version
65    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'; //DEBUG
72    this.log.debug(`Searching for datasetId: ${username}/${datasetName}`);
73    const existingDataset = await this.apifyClient
74      .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.apifyClient
87        .actor('tri_angle/new-fast-airbnb-scraper')
88        .call(actorInput, { maxItems: 100 }); // DEBUG
89      await this.apifyClient
90        .dataset(actorRun.defaultDatasetId)
91        .update({ name: datasetName });
92      const dataset = await this.apifyClient
93        .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.apifyClient
44      .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 function
17 */
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: 30
57    };
58    // checks for cached stored version
59    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.apifyClient
67      .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.apifyClient
76        .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.apifyClient
83        .actor('canadesk/duckduckgo-serp-api')
84        .call(actorInput);
85      dataset = await this.apifyClient
86        .dataset(actorRun.defaultDatasetId)
87        .listItems();
88      await this.apifyClient
89        .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 default
17 */
18const sampleInput = {
19  method: 'getAllItems',
20  output: '{"results":[""]}',
21};
22
23/**
24 * Tool that uses the WebsiteScraper function
25 */
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 version
53    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.apifyClient
61      .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.apifyClient
70        .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.apifyClient
80        .actor('zeeb0t/web-scraping-api---scrape-any-website')
81        .call(actorInput, callOptions);
82      dataset = await this.apifyClient
83        .dataset(actorRun.defaultDatasetId)
84        .listItems();
85      if (dataset.total > 0) {
86        await this.apifyClient
87          .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-vars
37  persistRun(_run: Run) {
38    return Promise.resolve();
39  }
40
41  /**
42   * Logs tokens usage in $.
43   * @returns void
44   */
45  override onLLMEnd(run: Run) {
46    const tokenUsage = run?.outputs?.llmOutput?.tokenUsage;
47    if (tokenUsage) {
48      const inputCostsUSD = this.modelCost.input
49        * (tokenUsage.promptTokens / 1000);
50      const outputCostsUSD = this.modelCost.output
51        * (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_time
58        && (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 tokens
3  output: number; // USD cost per 1000 tokens
4}
5
6export interface GPTModelConfig {
7  model: string;
8  maxTokens: number;
9  maxOutputTokens?: number;
10  interface: 'text' | 'chat';
11  cost: OpenaiAPICost; // USD cost per 1000 tokens
12}
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-09
18*/
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) === 0
35  ) {
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}

Pricing

Pricing model

Pay per event 

This Actor is paid per result. You are not charged for the Apify platform usage, but only a fixed price for each dataset of 1,000 items in the Actor outputs.

Actor start per 1 GB

$0.010

Flat fee for starting an Actor run for each 1 GB of memory.

Price per 1000 OpenAI tokens for gpt-4o

$0.010

Flat fee for every 1000 tokens (input/output) used with gpt-4o.

Price per 1000 OpenAI tokens for gpt-4o-mini

$0.001

Flat fee for every 1000 tokens (input/output) used with gpt-4o-mini.

Price per 1000 OpenAI tokens for gpt-3.5-turbo

$0.002

Flat fee for every 1000 tokens (input/output) used with gpt-3.5-turbo.

Price per Duck Duck Go search

$0.010

Flat fee for every Duck Duck Go search.

Price per web page scraped

$0.010

Flat fee for every web page scraped.

Price per Airbnb search result

$0.001

Flat fee for every result when searching Airbnb.

Price per TripAdvisor search result

$0.003

Flat fee for every result when searching TripAdvisor.