├── .eslintrc.js ├── .git-blame-ignore-revs ├── .github ├── CODE_OF_CONDUCT.md ├── ISSUE_TEMPLATE │ ├── BUG_REPORT.yml │ └── config.yml ├── pull_request_template.md └── workflows │ ├── commit-checker.yml │ └── node-publish.yml ├── .gitignore ├── .prettierrc.json ├── CONTRIBUTING.md ├── README.md ├── SECURITY.md ├── SUPPORT.md ├── babel.config.js ├── build ├── docs └── images │ ├── Sticker.png │ ├── anthropic.png │ ├── azure.png │ ├── bard.png │ ├── cohere.png │ ├── header.png │ ├── localai.png │ └── openai.png ├── examples ├── demo.ts ├── fallback.ts └── promptGeneration.ts ├── jest.config.ts ├── package-lock.json ├── package.json ├── scripts ├── check-version.cjs └── make-dist-package-json.cjs ├── src ├── _types │ ├── chatCompletionTypes.ts │ ├── generalTypes.ts │ ├── index.ts │ ├── portkeyConstructs.ts │ └── sharedTypes.ts ├── apiResource.ts ├── apis │ ├── admin.ts │ ├── apiKeys.ts │ ├── assistants.ts │ ├── audio.ts │ ├── batches.ts │ ├── betaChat.ts │ ├── chatCompletions.ts │ ├── collections.ts │ ├── completions.ts │ ├── configs.ts │ ├── createHeaders.ts │ ├── deleteMethod.ts │ ├── embeddings.ts │ ├── evals.ts │ ├── feedback.ts │ ├── files.ts │ ├── fineTuning.ts │ ├── generations.ts │ ├── getMethod.ts │ ├── images.ts │ ├── index.ts │ ├── labels.ts │ ├── logsExport.ts │ ├── models.ts │ ├── moderations.ts │ ├── postMethod.ts │ ├── putMethod.ts │ ├── realtime.ts │ ├── responses.ts │ ├── threads.ts │ ├── uploads.ts │ ├── vectorStores.ts │ └── virtualKeys.ts ├── baseClient.ts ├── beta │ └── realtime │ │ ├── index.ts │ │ └── ws.ts ├── client.ts ├── constants.ts ├── core.ts ├── error.ts ├── getAudioDuration.ts ├── index.ts ├── streaming.ts ├── utils.ts └── version.ts ├── tests ├── assistants │ └── openai.test.ts ├── audio │ ├── openai.test.ts │ └── speech.mp3 ├── chat │ ├── anthropic.test.ts │ ├── anyscale.test.ts │ └── openai.test.ts ├── completion │ ├── anthropic.test.ts │ ├── anyscale.test.ts │ └── openai.test.ts ├── images │ ├── image.png │ ├── imageMask.png │ └── openai.test.ts └── moderations │ └── openai.test.ts └── tsconfig.json /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | parser: "@typescript-eslint/parser", 4 | plugins: ["@typescript-eslint"], 5 | extends: [ 6 | "eslint:recommended", 7 | "plugin:@typescript-eslint/recommended", 8 | "prettier", 9 | ], 10 | ignorePatterns: [ 11 | '**/node_modules/', 12 | '**/dist/', 13 | '**/*.test.js', 14 | '**/examples/*', 15 | '**/src/index.ts', 16 | '**/src/error.ts', 17 | '**/src/streaming.ts', 18 | ], 19 | rules: { 20 | "no-case-declarations": "warn", 21 | "no-console": "warn", 22 | "no-duplicate-imports": "error", 23 | "@typescript-eslint/no-unused-vars": "error", 24 | "prefer-const": "error", 25 | "@typescript-eslint/no-explicit-any": "off", 26 | 'no-undef': 'off', 27 | '@typescript-eslint/no-var-requires': 'off', 28 | '@typescript-eslint/ban-ts-comment': 'off', 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /.git-blame-ignore-revs: -------------------------------------------------------------------------------- 1 | 3733b78475e0f1f1b408e4ae320c9fb7b1e99e2e 2 | 19be3cf227899e06da9b7d62b37bac706f0fb83e 3 | 776f2935326af1318ea3a9f02efa937e1439d06b 4 | d84d88b12085f3fda8ab637daaec23d625bb32ad 5 | 61364973c9df8a412d813e7e5112b919cf5c5484 6 | a0d30fdfd36d485baf635c4bcfb58c6827d9d2ea 7 | 20b71deeff8ec4a009030d79fa8228178d915acc -------------------------------------------------------------------------------- /.github/CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | We as members, contributors, and leaders pledge to make participation in our 6 | community a harassment-free experience for everyone, regardless of age, body 7 | size, visible or invisible disability, ethnicity, sex characteristics, gender 8 | identity and expression, level of experience, education, socio-economic status, 9 | nationality, personal appearance, race, religion, or sexual identity 10 | and orientation. 11 | 12 | We pledge to act and interact in ways that contribute to an open, welcoming, 13 | diverse, inclusive, and healthy community. 14 | 15 | ## Our Standards 16 | 17 | Examples of behavior that contributes to a positive environment for our 18 | community include: 19 | 20 | * Demonstrating empathy and kindness toward other people 21 | * Being respectful of differing opinions, viewpoints, and experiences 22 | * Giving and gracefully accepting constructive feedback 23 | * Accepting responsibility and apologizing to those affected by our mistakes, 24 | and learning from the experience 25 | * Focusing on what is best not just for us as individuals, but for the 26 | overall community 27 | 28 | Examples of unacceptable behavior include: 29 | 30 | * The use of sexualized language or imagery, and sexual attention or 31 | advances of any kind 32 | * Trolling, insulting or derogatory comments, and personal or political attacks 33 | * Public or private harassment 34 | * Publishing others' private information, such as a physical or email 35 | address, without their explicit permission 36 | * Other conduct which could reasonably be considered inappropriate in a 37 | professional setting 38 | 39 | ## Enforcement Responsibilities 40 | 41 | Community leaders are responsible for clarifying and enforcing our standards of 42 | acceptable behavior and will take appropriate and fair corrective action in 43 | response to any behavior that they deem inappropriate, threatening, offensive, 44 | or harmful. 45 | 46 | Community leaders have the right and responsibility to remove, edit, or reject 47 | comments, commits, code, wiki edits, issues, and other contributions that are 48 | not aligned to this Code of Conduct, and will communicate reasons for moderation 49 | decisions when appropriate. 50 | 51 | ## Scope 52 | 53 | This Code of Conduct applies within all community spaces, and also applies when 54 | an individual is officially representing the community in public spaces. 55 | Examples of representing our community include using an official e-mail address, 56 | posting via an official social media account, or acting as an appointed 57 | representative at an online or offline event. 58 | 59 | ## Enforcement 60 | 61 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 62 | reported to the community leaders responsible for enforcement at 63 | support@portkey.ai. 64 | All complaints will be reviewed and investigated promptly and fairly. 65 | 66 | All community leaders are obligated to respect the privacy and security of the 67 | reporter of any incident. 68 | 69 | ## Enforcement Guidelines 70 | 71 | Community leaders will follow these Community Impact Guidelines in determining 72 | the consequences for any action they deem in violation of this Code of Conduct: 73 | 74 | ### 1. Correction 75 | 76 | **Community Impact**: Use of inappropriate language or other behavior deemed 77 | unprofessional or unwelcome in the community. 78 | 79 | **Consequence**: A private, written warning from community leaders, providing 80 | clarity around the nature of the violation and an explanation of why the 81 | behavior was inappropriate. A public apology may be requested. 82 | 83 | ### 2. Warning 84 | 85 | **Community Impact**: A violation through a single incident or series 86 | of actions. 87 | 88 | **Consequence**: A warning with consequences for continued behavior. No 89 | interaction with the people involved, including unsolicited interaction with 90 | those enforcing the Code of Conduct, for a specified period of time. This 91 | includes avoiding interactions in community spaces as well as external channels 92 | like social media. Violating these terms may lead to a temporary or 93 | permanent ban. 94 | 95 | ### 3. Temporary Ban 96 | 97 | **Community Impact**: A serious violation of community standards, including 98 | sustained inappropriate behavior. 99 | 100 | **Consequence**: A temporary ban from any sort of interaction or public 101 | communication with the community for a specified period of time. No public or 102 | private interaction with the people involved, including unsolicited interaction 103 | with those enforcing the Code of Conduct, is allowed during this period. 104 | Violating these terms may lead to a permanent ban. 105 | 106 | ### 4. Permanent Ban 107 | 108 | **Community Impact**: Demonstrating a pattern of violation of community 109 | standards, including sustained inappropriate behavior, harassment of an 110 | individual, or aggression toward or disparagement of classes of individuals. 111 | 112 | **Consequence**: A permanent ban from any sort of public interaction within 113 | the community. 114 | 115 | ## Attribution 116 | 117 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], 118 | version 2.0, available at 119 | https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. 120 | 121 | Community Impact Guidelines were inspired by [Mozilla's code of conduct 122 | enforcement ladder](https://github.com/mozilla/diversity). 123 | 124 | [homepage]: https://www.contributor-covenant.org 125 | 126 | For answers to common questions about this code of conduct, see the FAQ at 127 | https://www.contributor-covenant.org/faq. Translations are available at 128 | https://www.contributor-covenant.org/translations. -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/BUG_REPORT.yml: -------------------------------------------------------------------------------- 1 | name: Bug Report 2 | description: File a bug report 3 | title: "[Bug]: " 4 | labels: ["bug", "triage"] 5 | assignees: 6 | - noble-varghese 7 | body: 8 | - type: markdown 9 | attributes: 10 | value: | 11 | Thanks for taking the time to fill out this bug report! 12 | - type: input 13 | id: contact 14 | attributes: 15 | label: Contact Details 16 | description: How can we get in touch with you if we need more info? 17 | placeholder: ex. email@example.com 18 | validations: 19 | required: false 20 | - type: textarea 21 | id: what-happened 22 | attributes: 23 | label: What happened? 24 | description: Also tell us, what did you expect to happen? 25 | placeholder: Tell us what you see! 26 | value: "A bug happened!" 27 | validations: 28 | required: true 29 | - type: dropdown 30 | id: version 31 | attributes: 32 | label: Version 33 | description: What version of our software are you running? 34 | options: 35 | - 0.1.xx (Default) 36 | default: 0 37 | validations: 38 | required: true 39 | - type: textarea 40 | id: logs 41 | attributes: 42 | label: Relevant log output 43 | description: Please copy and paste any relevant log output. This will be automatically formatted into code, so no need for backticks. 44 | render: shell 45 | - type: checkboxes 46 | id: terms 47 | attributes: 48 | label: Code of Conduct 49 | description: By submitting this issue, you agree to follow our [Code of Conduct](https://github.com/Portkey-AI/portkey-node-sdk/blob/main/.github/CODE_OF_CONDUCT.md) 50 | options: 51 | - label: I agree to follow this project's Code of Conduct 52 | required: true -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | blank_issues_enabled: true 2 | contact_links: 3 | - name: Portkey Community Support 4 | url: https://discord.com/invite/DD7vgKK299 5 | about: Please ask and answer questions here. 6 | - name: Portkey Bounty 7 | url: https://discord.com/invite/DD7vgKK299 8 | about: Please report security vulnerabilities here. -------------------------------------------------------------------------------- /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | **Title:** 2 | 3 | **Description:** 4 | - Detailed change 1 5 | - Detailed change 2 6 | - ... 7 | 8 | **Motivation:** 9 | 10 | 11 | **Related Issues:** 12 | # -------------------------------------------------------------------------------- /.github/workflows/commit-checker.yml: -------------------------------------------------------------------------------- 1 | name: verify-conventional-commits 2 | 3 | on: [pull_request] 4 | 5 | jobs: 6 | conventional-commits-checker: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - name: verify conventional commits 10 | uses: taskmedia/action-conventional-commits@v1.1.8 11 | -------------------------------------------------------------------------------- /.github/workflows/node-publish.yml: -------------------------------------------------------------------------------- 1 | name: Publish Package to npmjs 2 | on: 3 | release: 4 | types: [published] 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - uses: actions/checkout@v4 10 | # Setup .npmrc file to publish to npm 11 | - uses: actions/setup-node@v3 12 | with: 13 | node-version: '20.x' 14 | registry-url: 'https://registry.npmjs.org' 15 | - run: npm i 16 | - run: npm ci 17 | - run: npm run build && npm publish 18 | env: 19 | NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | .pnpm-debug.log* 9 | 10 | # Diagnostic reports (https://nodejs.org/api/report.html) 11 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 12 | 13 | # Runtime data 14 | pids 15 | *.pid 16 | *.seed 17 | *.pid.lock 18 | 19 | # Directory for instrumented libs generated by jscoverage/JSCover 20 | lib-cov 21 | 22 | # Coverage directory used by tools like istanbul 23 | coverage 24 | *.lcov 25 | 26 | # nyc test coverage 27 | .nyc_output 28 | 29 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 30 | .grunt 31 | 32 | # Bower dependency directory (https://bower.io/) 33 | bower_components 34 | 35 | # node-waf configuration 36 | .lock-wscript 37 | 38 | # Compiled binary addons (https://nodejs.org/api/addons.html) 39 | build/Release 40 | 41 | # Dependency directories 42 | node_modules/ 43 | jspm_packages/ 44 | 45 | # Snowpack dependency directory (https://snowpack.dev/) 46 | web_modules/ 47 | 48 | # TypeScript cache 49 | *.tsbuildinfo 50 | 51 | # Optional npm cache directory 52 | .npm 53 | 54 | # Optional eslint cache 55 | .eslintcache 56 | 57 | # Optional stylelint cache 58 | .stylelintcache 59 | 60 | # Microbundle cache 61 | .rpt2_cache/ 62 | .rts2_cache_cjs/ 63 | .rts2_cache_es/ 64 | .rts2_cache_umd/ 65 | 66 | # Optional REPL history 67 | .node_repl_history 68 | 69 | # Output of 'npm pack' 70 | *.tgz 71 | 72 | # Yarn Integrity file 73 | .yarn-integrity 74 | 75 | # dotenv environment variable files 76 | .env 77 | .env.development.local 78 | .env.test.local 79 | .env.production.local 80 | .env.local 81 | 82 | # parcel-bundler cache (https://parceljs.org/) 83 | .cache 84 | .parcel-cache 85 | 86 | # Next.js build output 87 | .next 88 | out 89 | 90 | # Nuxt.js build / generate output 91 | .nuxt 92 | dist 93 | 94 | # Gatsby files 95 | .cache/ 96 | # Comment in the public line in if your project uses Gatsby and not Next.js 97 | # https://nextjs.org/blog/next-9-1#public-directory-support 98 | # public 99 | 100 | # vuepress build output 101 | .vuepress/dist 102 | 103 | # vuepress v2.x temp and cache directory 104 | .temp 105 | .cache 106 | 107 | # Docusaurus cache and generated files 108 | .docusaurus 109 | 110 | # Serverless directories 111 | .serverless/ 112 | 113 | # FuseBox cache 114 | .fusebox/ 115 | 116 | # DynamoDB Local files 117 | .dynamodb/ 118 | 119 | # TernJS port file 120 | .tern-port 121 | 122 | # Stores VSCode versions used for testing VSCode extensions 123 | .vscode-test 124 | 125 | # yarn v2 126 | .yarn/cache 127 | .yarn/unplugged 128 | .yarn/build-state.yml 129 | .yarn/install-state.gz 130 | .pnp.* 131 | 132 | .DS_Store -------------------------------------------------------------------------------- /.prettierrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "semi": true, 3 | "tabWidth": 2, 4 | "printWidth": 80, 5 | "endOfLine": "lf", 6 | "singleQuote": true, 7 | "arrowParens": "always", 8 | "bracketSpacing": true, 9 | "trailingComma": "es5" 10 | } 11 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ## 🎉 Welcome 2 | Hello and thank you for considering contributing to the Portkey Node SDK! Whether you're reporting a bug, suggesting a feature, improving documentation, or writing code, your contributions are invaluable to us. 3 | 4 | ## 🚀 Quick Start 5 | 1. Fork the repository on Github. 6 | 2. Clone your forked repository to your machine. 7 | ```sh 8 | $ git clone https://github.com/YOUR_USERNAME/portkey-node-sdk.git 9 | ``` 10 | 3. 11 | 12 | ## 🖋 Types of Contributions 13 | 1. New integrations: Creating integrations for other LLM providers or vendors in general. 14 | 2. Bug fixes 15 | 3. SDK enhancements 16 | 17 | ## 🔄 Raising PRs 18 | 1. Once you are done with your changes, format and Lint your code by running: 19 | ```sh 20 | make format; make lint 21 | ``` 22 | 2. While raising your PRs, please prepend any of the following to your title: 23 | * `integration: ` for new integrations. 24 | * `improvement: ` for improvements or enhancements. 25 | * `bug: ` for bug fixes. 26 | 27 | ## 🤔 Getting Help 28 | Facing issues or have questions? Don't hesitate to share your doubts or questions on our [Discord Community](https://discord.com/invite/DD7vgKK299) - this is the quickest way to get support and connect with other contributors. 29 | 30 | ## 🚧 Release Process 31 | Releases are made as soon as possible to ensure that new features and fixes reach our users quickly. We follow a seamless CI/CD pipeline to ensure the smooth transition of code from development to production. 32 | 33 | ## 🎊 Your PR is Merged! 34 | All successful PRs are celebrated on our [Discord](https://discord.com/invite/DD7vgKK299) and are mentioned in the release notes, and significant contributions are highlighted on our [Twitter](https://twitter.com/PortkeyAI). Stay tuned for more bounties and goodies for contributors in the near future! -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 |
3 | 4 | ## Control Panel for AI Apps 5 | ```bash 6 | npm install portkey-ai 7 | ``` 8 |
9 | 10 | ## Features 11 | 12 | The Portkey SDK is built on top of the OpenAI SDK, allowing you to seamlessly integrate Portkey's advanced features while retaining full compatibility with OpenAI methods. With Portkey, you can enhance your interactions with OpenAI or any other OpenAI-like provider by leveraging robust monitoring, reliability, prompt management, and more features - without modifying much of your existing code. 13 | 14 | ### AI Gateway 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 |
Unified API Signature
If you've used OpenAI, you already know how to use Portkey with any other provider.
Interoperability
Write once, run with any provider. Switch between any model from_any provider seamlessly.
Automated Fallbacks & Retries
Ensure your application remains functional even if a primary service fails.
Load Balancing
Efficiently distribute incoming requests among multiple models.
Semantic Caching
Reduce costs and latency by intelligently caching results.
Virtual Keys
Secure your LLM API keys by storing them in Portkey vault and using disposable virtual keys.
Request Timeouts
Manage unpredictable LLM latencies effectively by setting custom request timeouts on requests.
32 | 33 | ### Observability 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 |
Logging
Keep track of all requests for monitoring and debugging.
Requests Tracing
Understand the journey of each request for optimization.
Custom Metadata
Segment and categorize requests for better insights.
Feedbacks
Collect and analyse weighted feedback on requests from users.
Analytics
Track your app & LLM's performance with 40+ production-critical metrics in a single place.
47 | 48 | ## **Usage** 49 | #### Prerequisites 50 | 1. [Sign up on Portkey](https://app.portkey.ai/) and grab your Portkey API Key 51 | 2. Add your [OpenAI key](https://platform.openai.com/api-keys) to Portkey's Virtual Keys page and keep it handy 52 | 53 | ```bash 54 | # Installing the SDK 55 | 56 | $ npm install portkey-ai 57 | $ export PORTKEY_API_KEY="PORTKEY_API_KEY" 58 | ``` 59 | 60 | #### Making a Request to OpenAI 61 | * Portkey fully adheres to the OpenAI SDK signature. You can instantly switch to Portkey and start using our production features right out of the box.
62 | * Just replace `import OpenAI from 'openai'` with `import Portkey from 'portkey-ai'`: 63 | ```js 64 | import Portkey from 'portkey-ai'; 65 | 66 | const portkey = new Portkey({ 67 | virtualKey: "VIRTUAL_KEY" 68 | }) 69 | 70 | async function main() { 71 | const chatCompletion = await portkey.chat.completions.create({ 72 | messages: [{ role: 'user', content: 'Say this is a test' }], 73 | model: 'gpt-4', 74 | }); 75 | 76 | console.log(chatCompletion.choices); 77 | }; 78 | 79 | main(); 80 | ``` 81 | 82 | #### [Check out Portkey docs for the full list of supported providers](https://portkey.ai/docs/welcome/what-is-portkey#ai-providers-supported) 83 | 84 | follow on Twitter 85 | Discord 86 | 87 | #### Contributing 88 | Get started by checking out Github issues. Email us at support@portkey.ai or just ping on Discord to chat. 89 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | ## Supported Versions 4 | 5 | | Version | Supported | 6 | | ------- | ------------------ | 7 | | 0.1.x | :white_check_mark: | 8 | | 1.x.x | :white_check_mark: | 9 | 10 | ## Reporting a Vulnerability 11 | 12 | Please report any security vulnerabilities at `support@portkey.ai`. 13 | -------------------------------------------------------------------------------- /SUPPORT.md: -------------------------------------------------------------------------------- 1 | ## How to file issues and get help 2 | 3 | This project uses GitHub Issues to track bugs and feature requests. Please search the existing 4 | issues before filing new issues to avoid duplicates. For new issues, file your bug or 5 | feature request as a new Issue. 6 | 7 | For help and questions about using this project, please contact `support@portkey.ai`. Join the community discussions [here](https://discord.com/invite/DD7vgKK299). -------------------------------------------------------------------------------- /babel.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | presets: [ 3 | ['@babel/preset-env', { targets: { node: 'current' } }], 4 | '@babel/preset-typescript', 5 | ], 6 | }; -------------------------------------------------------------------------------- /build: -------------------------------------------------------------------------------- 1 | 2 | #!/usr/bin/env bash 3 | set -exuo pipefail 4 | 5 | node scripts/check-version.cjs 6 | 7 | # Build into dist and will publish the package from there, 8 | # so that src/resources/foo.ts becomes /resources/foo.js 9 | # This way importing from `"portkey-ai/apis/foo"` works 10 | # even with `"moduleResolution": "node"` 11 | 12 | rm -rf dist; mkdir dist 13 | 14 | # Copy only necessary non-source files to dist 15 | cp README.md dist/ 16 | # Copy the changelog and license files to dist 17 | for file in LICENSE CHANGELOG.md; do 18 | if [ -e "${file}" ]; then cp "${file}" dist; fi 19 | done 20 | 21 | # this converts the export map paths for the dist directory 22 | # and does a few other minor things 23 | node scripts/make-dist-package-json.cjs > dist/package.json 24 | 25 | # build to .js/.mjs/.d.ts files 26 | npm exec tsc-multi -------------------------------------------------------------------------------- /docs/images/Sticker.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Portkey-AI/portkey-node-sdk/d18d452e9eda254f993cc01c0a8b67ad8a5eb812/docs/images/Sticker.png -------------------------------------------------------------------------------- /docs/images/anthropic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Portkey-AI/portkey-node-sdk/d18d452e9eda254f993cc01c0a8b67ad8a5eb812/docs/images/anthropic.png -------------------------------------------------------------------------------- /docs/images/azure.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Portkey-AI/portkey-node-sdk/d18d452e9eda254f993cc01c0a8b67ad8a5eb812/docs/images/azure.png -------------------------------------------------------------------------------- /docs/images/bard.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Portkey-AI/portkey-node-sdk/d18d452e9eda254f993cc01c0a8b67ad8a5eb812/docs/images/bard.png -------------------------------------------------------------------------------- /docs/images/cohere.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Portkey-AI/portkey-node-sdk/d18d452e9eda254f993cc01c0a8b67ad8a5eb812/docs/images/cohere.png -------------------------------------------------------------------------------- /docs/images/header.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Portkey-AI/portkey-node-sdk/d18d452e9eda254f993cc01c0a8b67ad8a5eb812/docs/images/header.png -------------------------------------------------------------------------------- /docs/images/localai.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Portkey-AI/portkey-node-sdk/d18d452e9eda254f993cc01c0a8b67ad8a5eb812/docs/images/localai.png -------------------------------------------------------------------------------- /docs/images/openai.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Portkey-AI/portkey-node-sdk/d18d452e9eda254f993cc01c0a8b67ad8a5eb812/docs/images/openai.png -------------------------------------------------------------------------------- /examples/demo.ts: -------------------------------------------------------------------------------- 1 | import { config } from 'dotenv'; 2 | import Portkey from '../src'; 3 | 4 | config({ override: true }) 5 | 6 | // Initialize the Portkey client 7 | const portkey = new Portkey({ 8 | apiKey: process.env["PORTKEY_API_KEY"] ?? "", 9 | baseURL: "https://api.portkeydev.com/v1", 10 | provider: "openai", 11 | virtualKey: process.env["OPENAI_VIRTUAL_KEY"] ?? "" 12 | }); 13 | 14 | // Generate a text completion 15 | async function getTextCompletion() { 16 | const completion = await portkey.completions.create({ 17 | prompt: "Say this is a test", 18 | model: "gpt-3.5-turbo-instruct", 19 | }); 20 | 21 | console.log(completion.choices[0]?.text); 22 | } 23 | getTextCompletion(); -------------------------------------------------------------------------------- /examples/fallback.ts: -------------------------------------------------------------------------------- 1 | // import { Portkey } from "../src"; 2 | 3 | // const portkey = new Portkey({ 4 | // apiKey:"your-portkey-api-key", 5 | // mode: "fallback", 6 | // llms: [ 7 | // { provider: "openai", virtual_key: "open-ai-key-1234", trace_id: "1234", metadata: { hello: "world" } }, 8 | // { provider: "cohere", virtual_key: "cohere-api-key-1234", trace_id: "1234", metadata: { hello: "world" } }, 9 | // ] 10 | // }); 11 | 12 | // async function main() { 13 | // const chatCompletion = await portkey.chatCompletions.create({ 14 | // messages: [{ role: 'user', content: 'Say this is a test' }], 15 | // }); 16 | 17 | // console.log(chatCompletion.choices); 18 | // }; 19 | 20 | // main(); -------------------------------------------------------------------------------- /examples/promptGeneration.ts: -------------------------------------------------------------------------------- 1 | // import { Portkey } from "../src"; 2 | 3 | // const portkey = new Portkey({ 4 | // mode: "fallback" 5 | // }); 6 | 7 | // async function main() { 8 | // const chatCompletion = await portkey.generations.create({ 9 | // promptId: "your-prompt-id", 10 | // // variables: {hello: "world"} # Add variables if required 11 | // }); 12 | 13 | // console.log(chatCompletion.data); 14 | // }; 15 | 16 | // main(); -------------------------------------------------------------------------------- /jest.config.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * For a detailed explanation regarding each configuration property, visit: 3 | * https://jestjs.io/docs/configuration 4 | */ 5 | 6 | import type {Config} from 'jest'; 7 | 8 | const config: Config = { 9 | // All imported modules in your tests should be mocked automatically 10 | // automock: false, 11 | 12 | // Stop running tests after `n` failures 13 | // bail: 0, 14 | 15 | // The directory where Jest should store its cached dependency information 16 | // cacheDirectory: "/private/var/folders/6m/626d82td1x14mk51p36cfvz80000gn/T/jest_dx", 17 | 18 | // Automatically clear mock calls, instances, contexts and results before every test 19 | clearMocks: true, 20 | 21 | // Indicates whether the coverage information should be collected while executing the test 22 | collectCoverage: true, 23 | 24 | // An array of glob patterns indicating a set of files for which coverage information should be collected 25 | // collectCoverageFrom: undefined, 26 | 27 | // The directory where Jest should output its coverage files 28 | coverageDirectory: "coverage", 29 | 30 | // An array of regexp pattern strings used to skip coverage collection 31 | // coveragePathIgnorePatterns: [ 32 | // "/node_modules/" 33 | // ], 34 | 35 | // Indicates which provider should be used to instrument code for coverage 36 | // coverageProvider: "babel", 37 | 38 | // A list of reporter names that Jest uses when writing coverage reports 39 | // coverageReporters: [ 40 | // "json", 41 | // "text", 42 | // "lcov", 43 | // "clover" 44 | // ], 45 | 46 | // An object that configures minimum threshold enforcement for coverage results 47 | // coverageThreshold: undefined, 48 | 49 | // A path to a custom dependency extractor 50 | // dependencyExtractor: undefined, 51 | 52 | // Make calling deprecated APIs throw helpful error messages 53 | // errorOnDeprecated: false, 54 | 55 | // The default configuration for fake timers 56 | // fakeTimers: { 57 | // "enableGlobally": false 58 | // }, 59 | 60 | // Default timeout of a test in milliseconds 61 | // testTimeout: 20000, 62 | 63 | // Force coverage collection from ignored files using an array of glob patterns 64 | // forceCoverageMatch: [], 65 | 66 | // A path to a module which exports an async function that is triggered once before all test suites 67 | // globalSetup: undefined, 68 | 69 | // A path to a module which exports an async function that is triggered once after all test suites 70 | // globalTeardown: undefined, 71 | 72 | // A set of global variables that need to be available in all test environments 73 | // globals: {}, 74 | 75 | // The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers. 76 | // maxWorkers: "50%", 77 | 78 | // An array of directory names to be searched recursively up from the requiring module's location 79 | // moduleDirectories: [ 80 | // "node_modules" 81 | // ], 82 | 83 | // An array of file extensions your modules use 84 | // moduleFileExtensions: [ 85 | // "js", 86 | // "mjs", 87 | // "cjs", 88 | // "jsx", 89 | // "ts", 90 | // "tsx", 91 | // "json", 92 | // "node" 93 | // ], 94 | 95 | // A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module 96 | moduleNameMapper: { 97 | '^portkey-ai$': '/src/index.ts', 98 | '^portkey-ai/(.*)$': '/src/$1', 99 | }, 100 | 101 | // An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader 102 | modulePathIgnorePatterns: [ 103 | '/dist/', 104 | ], 105 | 106 | // Activates notifications for test results 107 | // notify: false, 108 | 109 | // An enum that specifies notification mode. Requires { notify: true } 110 | // notifyMode: "failure-change", 111 | 112 | // A preset that is used as a base for Jest's configuration 113 | preset: 'ts-jest', 114 | 115 | // Run tests from one or more projects 116 | // projects: undefined, 117 | 118 | // Use this configuration option to add custom reporters to Jest 119 | // reporters: undefined, 120 | 121 | // Automatically reset mock state before every test 122 | // resetMocks: false, 123 | 124 | // Reset the module registry before running each individual test 125 | // resetModules: false, 126 | 127 | // A path to a custom resolver 128 | // resolver: undefined, 129 | 130 | // Automatically restore mock state and implementation before every test 131 | // restoreMocks: false, 132 | 133 | // The root directory that Jest should scan for tests and modules within 134 | // rootDir: undefined, 135 | 136 | // A list of paths to directories that Jest should use to search for files in 137 | // roots: [ 138 | // "" 139 | // ], 140 | 141 | // Allows you to use a custom runner instead of Jest's default test runner 142 | // runner: "jest-runner", 143 | 144 | // The paths to modules that run some code to configure or set up the testing environment before each test 145 | // setupFiles: [], 146 | 147 | // A list of paths to modules that run some code to configure or set up the testing framework before each test 148 | // setupFilesAfterEnv: [], 149 | 150 | // The number of seconds after which a test is considered as slow and reported as such in the results. 151 | // slowTestThreshold: 5, 152 | 153 | // A list of paths to snapshot serializer modules Jest should use for snapshot testing 154 | // snapshotSerializers: [], 155 | 156 | // The test environment that will be used for testing 157 | // testEnvironment: "jest-environment-node", 158 | 159 | // Options that will be passed to the testEnvironment 160 | // testEnvironmentOptions: {}, 161 | 162 | // Adds a location field to test results 163 | // testLocationInResults: false, 164 | 165 | // The glob patterns Jest uses to detect test files 166 | // testMatch: [ 167 | // "**/__tests__/**/*.[jt]s?(x)", 168 | // "**/?(*.)+(spec|test).[tj]s?(x)" 169 | // ], 170 | 171 | // An array of regexp pattern strings that are matched against all test paths, matched tests are skipped 172 | // testPathIgnorePatterns: [ 173 | // "/node_modules/" 174 | // ], 175 | 176 | // The regexp pattern or array of patterns that Jest uses to detect test files 177 | // testRegex: [], 178 | 179 | // This option allows the use of a custom results processor 180 | // testResultsProcessor: undefined, 181 | 182 | // This option allows use of a custom test runner 183 | // testRunner: "jest-circus/runner", 184 | 185 | // A map from regular expressions to paths to transformers 186 | // transform: undefined, 187 | 188 | // An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation 189 | // transformIgnorePatterns: [ 190 | // "/node_modules/", 191 | // "\\.pnp\\.[^\\/]+$" 192 | // ], 193 | 194 | // An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them 195 | // unmockedModulePathPatterns: undefined, 196 | 197 | // Indicates whether each individual test should be reported during the run 198 | // verbose: undefined, 199 | 200 | // An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode 201 | // watchPathIgnorePatterns: [], 202 | 203 | // Whether to use watchman for file crawling 204 | // watchman: true, 205 | }; 206 | 207 | export default config; 208 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "portkey-ai", 3 | "version": "1.9.1", 4 | "description": "Node client library for the Portkey API", 5 | "types": "dist/src/index.d.ts", 6 | "main": "dist/src/index.js", 7 | "type": "commonjs", 8 | "repository": "github:Portkey-AI/portkey-node-sdk", 9 | "scripts": { 10 | "test": "jest", 11 | "build": "bash ./build", 12 | "format:check": "prettier --check \"src/**/*.ts\"", 13 | "format": "prettier --write \"src/**/*.ts\"", 14 | "lint:check": "eslint --ext ts,js .", 15 | "lint:fix": "eslint --fix --ext ts,js ." 16 | }, 17 | "imports": { 18 | "portkey-ai": ".", 19 | "portkey-ai/*": "./src/*" 20 | }, 21 | "keywords": [], 22 | "author": "portkey-ai ", 23 | "license": "MIT", 24 | "devDependencies": { 25 | "@babel/core": "^7.23.3", 26 | "@babel/preset-env": "^7.23.3", 27 | "@babel/preset-typescript": "^7.23.3", 28 | "@jest/globals": "^29.7.0", 29 | "@types/jest": "^29.5.10", 30 | "@types/node": "^20.7.0", 31 | "@types/ws": "^8.5.14", 32 | "@typescript-eslint/eslint-plugin": "^5.62.0", 33 | "@typescript-eslint/parser": "^5.62.0", 34 | "babel-jest": "^29.7.0", 35 | "eslint": "^8.54.0", 36 | "eslint-config-prettier": "^9.0.0", 37 | "eslint-plugin-prettier": "^4.2.1", 38 | "eslint-plugin-unused-imports": "^2.0.0", 39 | "jest": "^29.7.0", 40 | "prettier": "^3.1.0", 41 | "ts-jest": "^29.1.1", 42 | "ts-node": "^10.9.1", 43 | "typescript": "^5.3.2" 44 | }, 45 | "dependencies": { 46 | "agentkeepalive": "^4.5.0", 47 | "dotenv": "^16.3.1", 48 | "openai": "4.98.0", 49 | "ws": "^8.18.1" 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /scripts/check-version.cjs: -------------------------------------------------------------------------------- 1 | const fs = require('fs'); 2 | const path = require('path'); 3 | 4 | const main = () => { 5 | const pkg = require('../package.json'); 6 | const version = pkg['version']; 7 | if (!version) throw 'The version property is not set in the package.json file'; 8 | if (typeof version !== 'string') { 9 | throw `Unexpected type for the package.json version field; got ${typeof version}, expected string`; 10 | } 11 | 12 | const versionFile = path.resolve(__dirname, '..', 'src', 'version.ts'); 13 | const contents = fs.readFileSync(versionFile, 'utf8'); 14 | const output = contents.replace(/(export const VERSION = ')(.*)(')/g, `$1${version}$3`); 15 | fs.writeFileSync(versionFile, output); 16 | }; 17 | 18 | if (require.main === module) { 19 | main(); 20 | } -------------------------------------------------------------------------------- /scripts/make-dist-package-json.cjs: -------------------------------------------------------------------------------- 1 | const pkgJson = require('../package.json'); 2 | 3 | function processExportMap(m) { 4 | for (const key in m) { 5 | const value = m[key]; 6 | if (typeof value === 'string') m[key] = value.replace(/^\.\/dist\//, './'); 7 | else processExportMap(value); 8 | } 9 | } 10 | processExportMap(pkgJson.exports); 11 | 12 | for (const key of ['types', 'main', 'module']) { 13 | if (typeof pkgJson[key] === 'string') pkgJson[key] = pkgJson[key].replace(/^(\.\/)?dist\//, './'); 14 | } 15 | 16 | delete pkgJson.devDependencies; 17 | delete pkgJson.scripts.prepack; 18 | delete pkgJson.scripts.prepublishOnly; 19 | delete pkgJson.scripts.prepare; 20 | 21 | console.log(JSON.stringify(pkgJson, null, 2)); -------------------------------------------------------------------------------- /src/_types/chatCompletionTypes.ts: -------------------------------------------------------------------------------- 1 | import { 2 | ChatCompletionResponse, 3 | Choices, 4 | Message, 5 | Usage, 6 | Logprobs, 7 | ChatCompletionListResponse, 8 | } from '../apis/chatCompletions'; 9 | import { 10 | ChatCompletionMessageToolCall, 11 | ChatCompletionTokenLogprob, 12 | } from 'openai/resources/chat/completions'; 13 | 14 | export { 15 | type ChatCompletionResponse as ChatCompletion, 16 | type Choices as ChatCompletionChoices, 17 | type Message as ChatCompletionMessage, 18 | type Usage as ChatCompletionUsage, 19 | type Logprobs as ChatCompletionLogprobs, 20 | type ChatCompletionMessageToolCall as ChatCompletionMessageToolCall, 21 | type ChatCompletionTokenLogprob as ChatCompletionTokenLogprob, 22 | type ChatCompletionListResponse as ChatCompletionListResponse, 23 | }; 24 | -------------------------------------------------------------------------------- /src/_types/generalTypes.ts: -------------------------------------------------------------------------------- 1 | export type Headers = Record; 2 | 3 | export interface ApiClientInterface { 4 | apiKey?: string | null; 5 | baseURL?: string | null; 6 | virtualKey?: string | null | undefined; 7 | config?: Record | string | null | undefined; 8 | provider?: string | null | undefined; 9 | traceID?: string | null | undefined; 10 | metadata?: Record | null | undefined; 11 | Authorization?: string | null | undefined; 12 | cacheForceRefresh?: boolean | null | undefined; 13 | debug?: boolean | null | undefined; 14 | customHost?: string | null | undefined; 15 | openaiProject?: string | null | undefined; 16 | openaiOrganization?: string | null | undefined; 17 | awsSecretAccessKey?: string | null | undefined; 18 | awsAccessKeyId?: string | null | undefined; 19 | awsSessionToken?: string | null | undefined; 20 | awsRegion?: string | null | undefined; 21 | vertexProjectId?: string | null | undefined; 22 | vertexRegion?: string | null | undefined; 23 | workersAiAccountId?: string | null | undefined; 24 | azureResourceName?: string | null | undefined; 25 | azureDeploymentId?: string | null | undefined; 26 | azureApiVersion?: string | null | undefined; 27 | azureEndpointName?: string | null | undefined; 28 | huggingfaceBaseUrl?: string | null | undefined; 29 | forwardHeaders?: Array | null | undefined; 30 | cacheNamespace?: string | null | undefined; 31 | requestTimeout?: number | null | undefined; 32 | strictOpenAiCompliance?: boolean | null | undefined; 33 | anthropicBeta?: string | null | undefined; 34 | anthropicVersion?: string | null | undefined; 35 | mistralFimCompletion?: string | null | undefined; 36 | dangerouslyAllowBrowser?: boolean | null | undefined; 37 | vertexStorageBucketName?: string | null | undefined; 38 | providerFileName?: string | null | undefined; 39 | providerModel?: string | null | undefined; 40 | awsS3Bucket?: string | null | undefined; 41 | awsS3ObjectKey?: string | null | undefined; 42 | awsBedrockModel?: string | null | undefined; 43 | fireworksAccountId?: string | null | undefined; 44 | calculateAudioDuration?: boolean | null | undefined; 45 | [key: string]: any; 46 | } 47 | 48 | export interface APIResponseType { 49 | getHeaders: () => Record | null | undefined; 50 | } 51 | -------------------------------------------------------------------------------- /src/_types/index.ts: -------------------------------------------------------------------------------- 1 | export * as ChatCompletionTypes from './chatCompletionTypes'; 2 | -------------------------------------------------------------------------------- /src/_types/portkeyConstructs.ts: -------------------------------------------------------------------------------- 1 | export interface RetrySettings { 2 | attempts: number; 3 | on_status_codes: Array; 4 | } 5 | 6 | export interface FunctionInterface { 7 | name: string; 8 | description: string; 9 | parameters: string; 10 | } 11 | 12 | export interface ModelParams { 13 | model?: string; 14 | suffix?: string; 15 | max_tokens?: number; 16 | temperature?: number; 17 | top_k?: number; 18 | top_p?: number; 19 | n?: number; 20 | stop_sequences?: Array; 21 | timeout?: number; 22 | functions?: Array; 23 | function_call?: string | FunctionInterface; 24 | logprobs?: number; 25 | echo?: boolean; 26 | stop?: Array; 27 | presence_penalty?: number; 28 | frequency_penalty?: number; 29 | best_of?: number; 30 | logit_bias?: Record; 31 | user?: string; 32 | organization?: string; 33 | seed?: number; 34 | response_format?: any; 35 | service_tier?: string; 36 | top_logprobs?: number | null; 37 | parallel_tool_calls?: boolean; 38 | tools?: Array; 39 | tool_choice?: any; 40 | [key: string]: any; 41 | } 42 | 43 | export interface Message { 44 | role: string; 45 | content: string; 46 | } 47 | 48 | export interface Tool { 49 | type?: string; 50 | function?: Record; 51 | } 52 | -------------------------------------------------------------------------------- /src/_types/sharedTypes.ts: -------------------------------------------------------------------------------- 1 | export type Metadata = Record; 2 | 3 | export interface CursorPageParams { 4 | after?: string; 5 | limit?: number; 6 | [key: string]: any; 7 | } 8 | -------------------------------------------------------------------------------- /src/apiResource.ts: -------------------------------------------------------------------------------- 1 | import type { Portkey } from './index'; 2 | 3 | export class ApiResource { 4 | protected client: Portkey; 5 | protected post: Portkey['_post']; 6 | protected put: Portkey['_put']; 7 | protected getMethod: Portkey['_get']; 8 | protected deleteMethod: Portkey['_delete']; 9 | 10 | constructor(client: Portkey) { 11 | this.client = client; 12 | this.post = client._post.bind(client); 13 | this.put = client._put.bind(client); 14 | this.getMethod = client._get.bind(client); 15 | this.deleteMethod = client._delete.bind(client); // delete is a reserved word 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/apis/apiKeys.ts: -------------------------------------------------------------------------------- 1 | import { ApiResource } from '../apiResource'; 2 | import { APIResponseType, ApiClientInterface } from '../_types/generalTypes'; 3 | import { APIPromise, RequestOptions } from '../baseClient'; 4 | import { createHeaders } from './createHeaders'; 5 | import { toQueryParams } from '../utils'; 6 | 7 | export interface ApiKeysAddParams { 8 | type?: string; 9 | 'sub-type'?: string; 10 | name?: string; 11 | description?: string; 12 | workspace_id?: string; 13 | user_id?: string; 14 | rate_limits?: Record[]; 15 | usage_limits?: Record; 16 | scopes: string[]; 17 | defaults?: Record; 18 | expires_at?: any; 19 | [key: string]: any; 20 | } 21 | export interface ApiKeysAddResponse extends APIResponseType { 22 | id?: string; 23 | key?: string; 24 | object?: string; 25 | } 26 | export interface ApiKeysGetParams { 27 | id?: string; 28 | } 29 | export interface ApiKeysGetResponse extends APIResponseType { 30 | id?: string; 31 | key?: string; 32 | name?: string; 33 | description?: string; 34 | type?: string; 35 | organisation_id?: string; 36 | workspace_id?: string; 37 | user_id?: string; 38 | status?: string; 39 | created_at?: Date; 40 | last_updated_at?: Date; 41 | creation_mode?: string; 42 | rate_limits?: Record[]; 43 | usage_limits?: Record; 44 | reset_usage?: number; 45 | scopes?: string[]; 46 | defaults?: Record; 47 | object?: string; 48 | } 49 | export interface ApiKeysUpdateParams { 50 | id?: string; 51 | name?: string; 52 | description?: string; 53 | rate_limits?: Record[]; 54 | usage_limits?: Record; 55 | scopes?: string[]; 56 | defaults?: Record; 57 | expires_at?: any; 58 | [key: string]: any; 59 | } 60 | export interface ApiKeysListParams { 61 | page_size?: number; 62 | current_page?: number; 63 | workspace_id?: string; 64 | } 65 | export interface ApiKeysListResponse extends APIResponseType { 66 | total?: number; 67 | object?: string; 68 | data?: Record[]; 69 | } 70 | export interface ApiKeysDeleteParams { 71 | id?: string; 72 | } 73 | export class ApiKeys extends ApiResource { 74 | create( 75 | _body: ApiKeysAddParams, 76 | params?: ApiClientInterface, 77 | opts?: RequestOptions 78 | ): APIPromise { 79 | const body = _body; 80 | const type = body.type; 81 | const subType = body['sub-type']; 82 | if (params) { 83 | this.client.customHeaders = { 84 | ...this.client.customHeaders, 85 | ...createHeaders({ ...params }), 86 | }; 87 | } 88 | const response = this.post( 89 | `/api-keys/${type}/${subType}`, 90 | { body, ...opts } 91 | ); 92 | return response; 93 | } 94 | 95 | retrieve( 96 | _body: ApiKeysGetParams, 97 | params?: ApiClientInterface, 98 | opts?: RequestOptions 99 | ): APIPromise { 100 | const body = _body; 101 | const id = body.id; 102 | if (params) { 103 | this.client.customHeaders = { 104 | ...this.client.customHeaders, 105 | ...createHeaders({ ...params }), 106 | }; 107 | } 108 | const response = this.getMethod(`/api-keys/${id}`, { 109 | ...opts, 110 | }); 111 | return response; 112 | } 113 | 114 | update( 115 | _body: ApiKeysUpdateParams, 116 | params?: ApiClientInterface, 117 | opts?: RequestOptions 118 | ): APIPromise { 119 | const body = _body; 120 | const id = body.id; 121 | if (params) { 122 | this.client.customHeaders = { 123 | ...this.client.customHeaders, 124 | ...createHeaders({ ...params }), 125 | }; 126 | } 127 | const response = this.put(`/api-keys/${id}`, { body, ...opts }); 128 | return response; 129 | } 130 | list( 131 | _body: ApiKeysListParams, 132 | params?: ApiClientInterface, 133 | opts?: RequestOptions 134 | ): APIPromise { 135 | const body = _body; 136 | if (params) { 137 | this.client.customHeaders = { 138 | ...this.client.customHeaders, 139 | ...createHeaders({ ...params }), 140 | }; 141 | } 142 | const query = toQueryParams(body); 143 | const response = this.getMethod(`/api-keys${query}`, { 144 | ...opts, 145 | }); 146 | return response; 147 | } 148 | delete( 149 | _body: ApiKeysDeleteParams, 150 | params?: ApiClientInterface, 151 | opts?: RequestOptions 152 | ): APIPromise { 153 | const body = _body; 154 | const id = body.id; 155 | if (params) { 156 | this.client.customHeaders = { 157 | ...this.client.customHeaders, 158 | ...createHeaders({ ...params }), 159 | }; 160 | } 161 | const response = this.deleteMethod(`/api-keys/${id}`, { 162 | body, 163 | ...opts, 164 | }); 165 | return response; 166 | } 167 | } 168 | -------------------------------------------------------------------------------- /src/apis/assistants.ts: -------------------------------------------------------------------------------- 1 | import { CursorPageParams, Metadata } from '../_types/sharedTypes'; 2 | import { ApiClientInterface } from '../_types/generalTypes'; 3 | import { ApiResource } from '../apiResource'; 4 | import { RequestOptions } from '../baseClient'; 5 | import { finalResponse, initOpenAIClient, overrideConfig } from '../utils'; 6 | import { createHeaders } from './createHeaders'; 7 | import { AssistantUpdateParams as oaiAssistantUpdateParams } from 'openai/resources/beta/assistants'; 8 | 9 | export interface AssistantCreateParams { 10 | model: string; 11 | description?: string | null; 12 | instructions?: string | null; 13 | metadata?: Metadata | null; 14 | name?: string | null; 15 | tools?: Array; 16 | response_format?: any | null; 17 | temperature?: number | null; 18 | tool_resources?: any | null; 19 | top_p?: number | null; 20 | [key: string]: any; 21 | } 22 | 23 | export interface FileCreateParams { 24 | file_id: string; 25 | } 26 | 27 | export interface FileListParams extends CursorPageParams { 28 | before?: string; 29 | order?: string; 30 | } 31 | 32 | export interface AssistantListParams extends CursorPageParams { 33 | before?: string; 34 | order?: string; 35 | } 36 | 37 | export interface AssistantUpdateParams { 38 | description?: string | null; 39 | file_ids?: Array; 40 | instructions?: string | null; 41 | metadata?: Metadata | null; 42 | model?: string; 43 | name?: string | null; 44 | tools?: Array; 45 | response_format?: any | null; 46 | temperature?: number | null; 47 | tool_resources?: oaiAssistantUpdateParams.ToolResources | null; 48 | top_p?: number | null; 49 | [key: string]: any; 50 | } 51 | 52 | export class Assistants extends ApiResource { 53 | async create( 54 | _body: AssistantCreateParams, 55 | params?: ApiClientInterface, 56 | opts?: RequestOptions 57 | ): Promise { 58 | const body: AssistantCreateParams = _body; 59 | if (params) { 60 | const config = overrideConfig(this.client.config, params.config); 61 | this.client.customHeaders = { 62 | ...this.client.customHeaders, 63 | ...createHeaders({ ...params, config }), 64 | }; 65 | } 66 | 67 | const OAIclient = initOpenAIClient(this.client); 68 | 69 | const result = await OAIclient.beta.assistants 70 | .create(body, opts) 71 | .withResponse(); 72 | 73 | return finalResponse(result); 74 | } 75 | 76 | async list( 77 | _query?: AssistantListParams, 78 | params?: ApiClientInterface, 79 | opts?: RequestOptions 80 | ): Promise { 81 | const query: AssistantListParams | undefined = _query; 82 | if (params) { 83 | const config = overrideConfig(this.client.config, params.config); 84 | this.client.customHeaders = { 85 | ...this.client.customHeaders, 86 | ...createHeaders({ ...params, config }), 87 | }; 88 | } 89 | 90 | const OAIclient = initOpenAIClient(this.client); 91 | // eslint-disable-next-line @typescript-eslint/ban-ts-comment 92 | // @ts-ignore 93 | const result = await OAIclient.beta.assistants 94 | .list(query as any, opts) 95 | .withResponse(); 96 | 97 | return finalResponse(result); 98 | } 99 | 100 | async retrieve( 101 | assistantId: string, 102 | params?: ApiClientInterface, 103 | opts?: RequestOptions 104 | ): Promise { 105 | if (params) { 106 | const config = overrideConfig(this.client.config, params.config); 107 | this.client.customHeaders = { 108 | ...this.client.customHeaders, 109 | ...createHeaders({ ...params, config }), 110 | }; 111 | } 112 | 113 | const OAIclient = initOpenAIClient(this.client); 114 | 115 | const result = await OAIclient.beta.assistants 116 | .retrieve(assistantId, opts) 117 | .withResponse(); 118 | 119 | return finalResponse(result); 120 | } 121 | 122 | async update( 123 | assistantId: string, 124 | _body: AssistantUpdateParams, 125 | params?: ApiClientInterface, 126 | opts?: RequestOptions 127 | ): Promise { 128 | const body: AssistantUpdateParams = _body; 129 | if (params) { 130 | const config = overrideConfig(this.client.config, params.config); 131 | this.client.customHeaders = { 132 | ...this.client.customHeaders, 133 | ...createHeaders({ ...params, config }), 134 | }; 135 | } 136 | 137 | const OAIclient = initOpenAIClient(this.client); 138 | 139 | const result = await OAIclient.beta.assistants 140 | .update(assistantId, body, opts) 141 | .withResponse(); 142 | 143 | return finalResponse(result); 144 | } 145 | 146 | async del( 147 | assistantId: string, 148 | params?: ApiClientInterface, 149 | opts?: RequestOptions 150 | ): Promise { 151 | if (params) { 152 | const config = overrideConfig(this.client.config, params.config); 153 | this.client.customHeaders = { 154 | ...this.client.customHeaders, 155 | ...createHeaders({ ...params, config }), 156 | }; 157 | } 158 | 159 | const OAIclient = initOpenAIClient(this.client); 160 | 161 | const result = await OAIclient.beta.assistants 162 | .del(assistantId, opts) 163 | .withResponse(); 164 | 165 | return finalResponse(result); 166 | } 167 | } 168 | -------------------------------------------------------------------------------- /src/apis/audio.ts: -------------------------------------------------------------------------------- 1 | import { 2 | Transcription, 3 | TranscriptionCreateParams, 4 | TranscriptionCreateParamsNonStreaming, 5 | TranscriptionCreateParamsStreaming, 6 | TranscriptionCreateResponse, 7 | TranscriptionStreamEvent, 8 | TranscriptionVerbose, 9 | } from 'openai/resources/audio/transcriptions'; 10 | import { ApiClientInterface } from '../_types/generalTypes'; 11 | import { ApiResource } from '../apiResource'; 12 | import { APIPromise, RequestOptions } from '../baseClient'; 13 | import { finalResponse, initOpenAIClient, overrideConfig } from '../utils'; 14 | import { createHeaders } from './createHeaders'; 15 | import { TranslationCreateParams } from 'openai/resources/audio/translations'; 16 | import { SpeechCreateParams } from 'openai/resources/audio/speech'; 17 | import { Stream } from '../streaming'; 18 | import { AUDIO_FILE_DURATION_HEADER } from '../constants'; 19 | import getAudioFileDuration from '../getAudioDuration'; 20 | import { isRunningInBrowser } from '../core'; 21 | 22 | export class Audio extends ApiResource { 23 | transcriptions: transcriptions; 24 | translations: translations; 25 | speech: speech; 26 | 27 | constructor(client: any) { 28 | super(client); 29 | this.transcriptions = new transcriptions(client); 30 | this.translations = new translations(client); 31 | this.speech = new speech(client); 32 | } 33 | } 34 | 35 | export class transcriptions extends ApiResource { 36 | create( 37 | body: TranscriptionCreateParamsNonStreaming<'json' | undefined>, 38 | params?: ApiClientInterface, 39 | opts?: RequestOptions 40 | ): Promise>; 41 | create( 42 | body: TranscriptionCreateParamsNonStreaming<'verbose_json'>, 43 | params?: ApiClientInterface, 44 | opts?: RequestOptions 45 | ): Promise>; 46 | create( 47 | body: TranscriptionCreateParamsNonStreaming<'srt' | 'vtt' | 'text'>, 48 | params?: ApiClientInterface, 49 | opts?: RequestOptions 50 | ): Promise>; 51 | create( 52 | body: TranscriptionCreateParamsNonStreaming, 53 | params?: ApiClientInterface, 54 | opts?: RequestOptions 55 | ): Promise>; 56 | create( 57 | body: TranscriptionCreateParamsStreaming, 58 | params?: ApiClientInterface, 59 | opts?: RequestOptions 60 | ): Promise>>; 61 | create( 62 | body: TranscriptionCreateParamsStreaming, 63 | params?: ApiClientInterface, 64 | opts?: RequestOptions 65 | ): Promise< 66 | | APIPromise 67 | | APIPromise 68 | | APIPromise> 69 | >; 70 | async create( 71 | body: TranscriptionCreateParams, 72 | params?: ApiClientInterface, 73 | opts?: RequestOptions 74 | ): Promise< 75 | | APIPromise 76 | | APIPromise 77 | | APIPromise> 78 | > { 79 | // @ts-ignore 80 | const path = body.file?.path; 81 | if (path && this.client.calculateAudioDuration && !isRunningInBrowser()) { 82 | const duration = await getAudioFileDuration(path); 83 | if (duration) { 84 | params = { 85 | ...params, 86 | [AUDIO_FILE_DURATION_HEADER]: duration, 87 | }; 88 | } 89 | } 90 | if (params) { 91 | const config = overrideConfig(this.client.config, params.config); 92 | this.client.customHeaders = { 93 | ...this.client.customHeaders, 94 | ...createHeaders({ ...params, config }), 95 | }; 96 | } 97 | const OAIclient = initOpenAIClient(this.client); 98 | 99 | const response = OAIclient.audio.transcriptions.create(body as any, opts); 100 | 101 | return response as any; 102 | } 103 | } 104 | 105 | export class translations extends ApiResource { 106 | async create( 107 | _body: TranslationCreateBody, 108 | params?: ApiClientInterface, 109 | opts?: RequestOptions 110 | ): Promise { 111 | const body: any = _body; 112 | const path = body.file?.path; 113 | if (path && this.client.calculateAudioDuration && !isRunningInBrowser()) { 114 | const duration = await getAudioFileDuration(path); 115 | if (duration) { 116 | params = { 117 | ...params, 118 | [AUDIO_FILE_DURATION_HEADER]: duration, 119 | }; 120 | } 121 | } 122 | if (params) { 123 | const config = overrideConfig(this.client.config, params.config); 124 | this.client.customHeaders = { 125 | ...this.client.customHeaders, 126 | ...createHeaders({ ...params, config }), 127 | }; 128 | } 129 | const OAIclient = initOpenAIClient(this.client); 130 | const response = await OAIclient.audio.translations 131 | .create(body, opts) 132 | .withResponse(); 133 | return finalResponse(response); 134 | } 135 | } 136 | 137 | export class speech extends ApiResource { 138 | async create( 139 | _body: SpeechCreateBody, 140 | params?: ApiClientInterface, 141 | opts?: RequestOptions 142 | ): Promise { 143 | const body: SpeechCreateBody = _body; 144 | if (params) { 145 | const config = overrideConfig(this.client.config, params.config); 146 | this.client.customHeaders = { 147 | ...this.client.customHeaders, 148 | ...createHeaders({ ...params, config }), 149 | }; 150 | } 151 | const OAIclient = initOpenAIClient(this.client); 152 | const response = await OAIclient.audio.speech.create(body, opts); 153 | return response; 154 | } 155 | } 156 | 157 | export interface TranslationCreateBody extends TranslationCreateParams { 158 | [key: string]: any; 159 | } 160 | 161 | export interface SpeechCreateBody extends SpeechCreateParams { 162 | [key: string]: any; 163 | } 164 | -------------------------------------------------------------------------------- /src/apis/batches.ts: -------------------------------------------------------------------------------- 1 | import { BatchCreateParams, BatchListParams } from 'openai/resources/batches'; 2 | import { ApiClientInterface } from '../_types/generalTypes'; 3 | import { ApiResource } from '../apiResource'; 4 | import { RequestOptions } from '../baseClient'; 5 | import { finalResponse, initOpenAIClient, overrideConfig } from '../utils'; 6 | import { createHeaders } from './createHeaders'; 7 | 8 | export class Batches extends ApiResource { 9 | async create( 10 | _body: BatchCreateBody, 11 | params?: ApiClientInterface, 12 | opts?: RequestOptions 13 | ): Promise { 14 | const body: BatchCreateBody = _body; 15 | if (params) { 16 | const config = overrideConfig(this.client.config, params.config); 17 | this.client.customHeaders = { 18 | ...this.client.customHeaders, 19 | ...createHeaders({ ...params, config }), 20 | }; 21 | } 22 | 23 | const OAIclient = initOpenAIClient(this.client); 24 | 25 | const result = await OAIclient.batches.create(body, opts).withResponse(); 26 | return finalResponse(result); 27 | } 28 | 29 | async retrieve( 30 | batchId: string, 31 | params?: ApiClientInterface, 32 | opts?: RequestOptions 33 | ): Promise { 34 | if (params) { 35 | const config = overrideConfig(this.client.config, params.config); 36 | this.client.customHeaders = { 37 | ...this.client.customHeaders, 38 | ...createHeaders({ ...params, config }), 39 | }; 40 | } 41 | 42 | const OAIclient = initOpenAIClient(this.client); 43 | 44 | const result = await OAIclient.batches 45 | .retrieve(batchId, opts) 46 | .withResponse(); 47 | return finalResponse(result); 48 | } 49 | 50 | async list( 51 | _query?: BatchListParams, 52 | params?: ApiClientInterface, 53 | opts?: RequestOptions 54 | ): Promise { 55 | const query: BatchListParams | undefined = _query; 56 | if (params) { 57 | const config = overrideConfig(this.client.config, params.config); 58 | this.client.customHeaders = { 59 | ...this.client.customHeaders, 60 | ...createHeaders({ ...params, config }), 61 | }; 62 | } 63 | 64 | const OAIclient = initOpenAIClient(this.client); 65 | 66 | const result = await OAIclient.batches.list(query, opts).withResponse(); 67 | return finalResponse(result); 68 | } 69 | 70 | async cancel( 71 | batchId: string, 72 | params?: ApiClientInterface, 73 | opts?: RequestOptions 74 | ): Promise { 75 | if (params) { 76 | const config = overrideConfig(this.client.config, params.config); 77 | this.client.customHeaders = { 78 | ...this.client.customHeaders, 79 | ...createHeaders({ ...params, config }), 80 | }; 81 | } 82 | 83 | const OAIclient = initOpenAIClient(this.client); 84 | const body = {}; 85 | const options = { body, ...opts }; 86 | 87 | const result = await OAIclient.batches 88 | .cancel(batchId, options) 89 | .withResponse(); 90 | return finalResponse(result); 91 | } 92 | 93 | output( 94 | batchId: string, 95 | params?: ApiClientInterface, 96 | opts?: RequestOptions 97 | ): Promise { 98 | if (params) { 99 | const config = overrideConfig(this.client.config, params.config); 100 | this.client.customHeaders = { 101 | ...this.client.customHeaders, 102 | ...createHeaders({ ...params, config }), 103 | }; 104 | } 105 | const response = this.getMethod(`/batches/${batchId}/output`, opts); 106 | return response; 107 | } 108 | } 109 | 110 | export interface BatchCreateBody extends BatchCreateParams { 111 | [key: string]: any; 112 | } 113 | -------------------------------------------------------------------------------- /src/apis/betaChat.ts: -------------------------------------------------------------------------------- 1 | import { ChatCompletionStreamParams } from 'openai/lib/ChatCompletionStream'; 2 | import { ApiClientInterface } from '../_types/generalTypes'; 3 | import { ApiResource } from '../apiResource'; 4 | import { RequestOptions } from '../baseClient'; 5 | import { initOpenAIClient, overrideConfig } from '../utils'; 6 | import { createHeaders } from './createHeaders'; 7 | import { 8 | ChatCompletionFunctionRunnerParams, 9 | ChatCompletionToolRunnerParams, 10 | } from 'openai/lib/ChatCompletionRunner'; 11 | import { 12 | ChatCompletionStreamingFunctionRunnerParams, 13 | ChatCompletionStreamingToolRunnerParams, 14 | } from 'openai/lib/ChatCompletionStreamingRunner'; 15 | import { ChatCompletionParseParams } from 'openai/resources/beta/chat/completions'; 16 | 17 | export class BetaChat extends ApiResource { 18 | completions: Completions; 19 | 20 | constructor(client: any) { 21 | super(client); 22 | this.completions = new Completions(client); 23 | } 24 | } 25 | 26 | export class Completions extends ApiResource { 27 | async parse( 28 | _body: Params, 29 | params?: ApiClientInterface, 30 | opts?: RequestOptions 31 | ): Promise { 32 | const body: Params = _body; 33 | 34 | if (params) { 35 | const config = overrideConfig(this.client.config, params.config); 36 | this.client.customHeaders = { 37 | ...this.client.customHeaders, 38 | ...createHeaders({ ...params, config }), 39 | }; 40 | } 41 | 42 | const OAIclient = initOpenAIClient(this.client); 43 | 44 | const result = await OAIclient.beta.chat.completions.parse(body, opts); 45 | return result; 46 | } 47 | 48 | async runFunctions( 49 | body: ChatCompletionFunctionRunnerParams, 50 | params?: ApiClientInterface, 51 | opts?: RequestOptions 52 | ): Promise; 53 | async runFunctions( 54 | body: ChatCompletionStreamingFunctionRunnerParams, 55 | params?: ApiClientInterface, 56 | opts?: RequestOptions 57 | ): Promise; 58 | async runFunctions( 59 | _body: 60 | | ChatCompletionFunctionRunnerParams 61 | | ChatCompletionStreamingFunctionRunnerParams, 62 | params?: ApiClientInterface, 63 | opts?: RequestOptions 64 | ): Promise { 65 | const body: any = _body; 66 | if (params) { 67 | const config = overrideConfig(this.client.config, params.config); 68 | this.client.customHeaders = { 69 | ...this.client.customHeaders, 70 | ...createHeaders({ ...params, config }), 71 | }; 72 | } 73 | 74 | const OAIclient = initOpenAIClient(this.client); 75 | 76 | const result = await OAIclient.beta.chat.completions.runFunctions( 77 | body, 78 | opts 79 | ); 80 | return result; 81 | } 82 | 83 | async runTools( 84 | body: ChatCompletionToolRunnerParams, 85 | params?: ApiClientInterface, 86 | opts?: RequestOptions 87 | ): Promise; 88 | async runTools( 89 | body: ChatCompletionStreamingToolRunnerParams, 90 | params?: ApiClientInterface, 91 | opts?: RequestOptions 92 | ): Promise; 93 | async runTools( 94 | _body: 95 | | ChatCompletionToolRunnerParams 96 | | ChatCompletionStreamingToolRunnerParams, 97 | params?: ApiClientInterface, 98 | opts?: RequestOptions 99 | ): Promise { 100 | const body: any = _body; 101 | if (params) { 102 | const config = overrideConfig(this.client.config, params.config); 103 | this.client.customHeaders = { 104 | ...this.client.customHeaders, 105 | ...createHeaders({ ...params, config }), 106 | }; 107 | } 108 | 109 | const OAIclient = initOpenAIClient(this.client); 110 | 111 | const result = await OAIclient.beta.chat.completions.runTools(body, opts); 112 | return result; 113 | } 114 | 115 | async stream( 116 | _body: ChatCompletionStreamParams, 117 | params?: ApiClientInterface, 118 | opts?: RequestOptions 119 | ): Promise { 120 | const body: ChatCompletionStreamParams = _body; 121 | if (params) { 122 | const config = overrideConfig(this.client.config, params.config); 123 | this.client.customHeaders = { 124 | ...this.client.customHeaders, 125 | ...createHeaders({ ...params, config }), 126 | }; 127 | } 128 | 129 | const OAIclient = initOpenAIClient(this.client); 130 | 131 | const result = await OAIclient.beta.chat.completions.stream(body, opts); 132 | return result; 133 | } 134 | } 135 | 136 | export type BaseFunctionsArgs = readonly (object | string)[]; 137 | -------------------------------------------------------------------------------- /src/apis/chatCompletions.ts: -------------------------------------------------------------------------------- 1 | import { 2 | ChatCompletionMessageToolCall, 3 | ChatCompletionStreamOptions, 4 | ChatCompletionTokenLogprob, 5 | } from 'openai/resources/chat/completions'; 6 | import { APIResponseType, ApiClientInterface } from '../_types/generalTypes'; 7 | import { ModelParams } from '../_types/portkeyConstructs'; 8 | import { ApiResource } from '../apiResource'; 9 | import { APIPromise, RequestOptions } from '../baseClient'; 10 | import { CHAT_COMPLETE_API } from '../constants'; 11 | import { Stream } from '../streaming'; 12 | import { overrideConfig } from '../utils'; 13 | import { createHeaders } from './createHeaders'; 14 | import { Metadata, CursorPageParams } from '../_types/sharedTypes'; 15 | 16 | export class Chat extends ApiResource { 17 | completions: ChatCompletions = new ChatCompletions(this.client); 18 | } 19 | 20 | class ChatCompletions extends ApiResource { 21 | messages = new ChatCompletionsMessages(this.client); 22 | 23 | create( 24 | _body: ChatCompletionsBodyNonStreaming, 25 | params?: ApiClientInterface, 26 | opts?: RequestOptions 27 | ): APIPromise; 28 | create( 29 | _body: ChatCompletionsBodyStreaming, 30 | params?: ApiClientInterface, 31 | opts?: RequestOptions 32 | ): APIPromise>; 33 | create( 34 | _body: ChatCompletionsBodyBase, 35 | params?: ApiClientInterface, 36 | opts?: RequestOptions 37 | ): APIPromise | ChatCompletion>; 38 | create( 39 | _body: ChatCompletionCreateParams, 40 | params?: ApiClientInterface, 41 | opts?: RequestOptions 42 | ): APIPromise | APIPromise> { 43 | const body = _body; 44 | // If config is present then override it. 45 | if (params) { 46 | const config = overrideConfig(this.client.config, params.config); 47 | this.client.customHeaders = { 48 | ...this.client.customHeaders, 49 | ...createHeaders({ ...params, config }), 50 | }; 51 | } 52 | 53 | const stream = _body.stream ?? false; 54 | return this.post(CHAT_COMPLETE_API, { 55 | body, 56 | ...opts, 57 | stream, 58 | }) as APIPromise | APIPromise>; 59 | } 60 | 61 | retrieve( 62 | completionId: string, 63 | params?: ApiClientInterface, 64 | opts?: RequestOptions 65 | ): APIPromise { 66 | if (params) { 67 | const config = overrideConfig(this.client.config, params.config); 68 | this.client.customHeaders = { 69 | ...this.client.customHeaders, 70 | ...createHeaders({ ...params, config }), 71 | }; 72 | } 73 | return this.getMethod( 74 | `${CHAT_COMPLETE_API}/${completionId}`, 75 | { 76 | ...opts, 77 | } 78 | ); 79 | } 80 | 81 | update( 82 | completionId: string, 83 | _body: ChatCompletionUpdateParams, 84 | params?: ApiClientInterface, 85 | opts?: RequestOptions 86 | ): APIPromise { 87 | const body = _body; 88 | if (params) { 89 | const config = overrideConfig(this.client.config, params.config); 90 | this.client.customHeaders = { 91 | ...this.client.customHeaders, 92 | ...createHeaders({ ...params, config }), 93 | }; 94 | } 95 | return this.post(`${CHAT_COMPLETE_API}/${completionId}`, { 96 | body, 97 | ...opts, 98 | }); 99 | } 100 | 101 | list( 102 | query?: ChatCompletionListParams, 103 | params?: ApiClientInterface, 104 | opts?: RequestOptions 105 | ): APIPromise { 106 | if (params) { 107 | const config = overrideConfig(this.client.config, params.config); 108 | this.client.customHeaders = { 109 | ...this.client.customHeaders, 110 | ...createHeaders({ ...params, config }), 111 | }; 112 | } 113 | return this.getMethod(`${CHAT_COMPLETE_API}`, { 114 | ...opts, 115 | ...query, 116 | }); 117 | } 118 | 119 | del( 120 | completionId: string, 121 | params?: ApiClientInterface, 122 | opts?: RequestOptions 123 | ): APIPromise { 124 | if (params) { 125 | const config = overrideConfig(this.client.config, params.config); 126 | this.client.customHeaders = { 127 | ...this.client.customHeaders, 128 | ...createHeaders({ ...params, config }), 129 | }; 130 | } 131 | return this.deleteMethod( 132 | `${CHAT_COMPLETE_API}/${completionId}`, 133 | { 134 | ...opts, 135 | } 136 | ); 137 | } 138 | } 139 | 140 | export class ChatCompletionsMessages extends ApiResource { 141 | list( 142 | completionId: string, 143 | query?: MessageListParams, 144 | params?: ApiClientInterface, 145 | opts?: RequestOptions 146 | ): APIPromise { 147 | if (params) { 148 | const config = overrideConfig(this.client.config, params.config); 149 | this.client.customHeaders = { 150 | ...this.client.customHeaders, 151 | ...createHeaders({ ...params, config }), 152 | }; 153 | } 154 | return this.getMethod( 155 | `${CHAT_COMPLETE_API}/${completionId}/messages`, 156 | { 157 | ...opts, 158 | ...query, 159 | } 160 | ); 161 | } 162 | } 163 | 164 | export interface ChatCompletionsBodyBase extends ModelParams { 165 | messages?: Array; 166 | response_format?: object; 167 | audio?: any | null; 168 | modalities?: Array | null; 169 | prediction?: any | null; 170 | reasoning_effort?: any; 171 | store?: boolean | null; 172 | metadata?: Record | null; 173 | max_completion_tokens?: number | null; 174 | } 175 | 176 | export interface ChatCompletionsBodyStreaming extends ChatCompletionsBodyBase { 177 | stream?: true; 178 | stream_options?: ChatCompletionStreamOptions; 179 | } 180 | 181 | export interface ChatCompletionsBodyNonStreaming 182 | extends ChatCompletionsBodyBase { 183 | stream?: false; 184 | } 185 | 186 | export type ChatCompletionCreateParams = 187 | | ChatCompletionsBodyNonStreaming 188 | | ChatCompletionsBodyStreaming; 189 | 190 | export interface Usage { 191 | prompt_tokens?: number; 192 | completion_tokens?: number; 193 | total_tokens?: number; 194 | [key: string]: any; 195 | } 196 | 197 | export interface Message { 198 | role: string; 199 | content: string | Array; 200 | refusal?: string; 201 | function_call?: any; 202 | tool_calls?: Array; 203 | tool_call_id?: string; 204 | [key: string]: any; 205 | } 206 | 207 | export interface Logprobs { 208 | content: Array | null; 209 | refusal: Array | null; 210 | [key: string]: any; 211 | } 212 | 213 | export interface Choices { 214 | index?: number; 215 | message?: Message; 216 | delta?: Message; 217 | finish_reason?: string; 218 | logprobs?: Logprobs; 219 | [key: string]: any; 220 | } 221 | 222 | export interface ChatCompletion extends APIResponseType { 223 | id: string; 224 | object: string; 225 | created: number; 226 | model: string; 227 | choices: Array; 228 | usage: Usage; 229 | service_tier?: string; 230 | system_fingerprint?: string; 231 | [key: string]: any; 232 | } 233 | 234 | export interface ChatCompletionResponse { 235 | id: string; 236 | object: string; 237 | created: number; 238 | model: string; 239 | choices: Array; 240 | usage: Usage; 241 | service_tier?: string; 242 | system_fingerprint?: string; 243 | [key: string]: any; 244 | } 245 | 246 | export interface ChatCompletionUpdateParams { 247 | metadata: Metadata | null; 248 | } 249 | 250 | export interface ChatCompletionListParams extends CursorPageParams { 251 | metadata?: Metadata | null; 252 | model?: string; 253 | order?: 'asc' | 'desc'; 254 | } 255 | 256 | export interface ChatCompletionListResponse extends APIResponseType { 257 | data: Array; 258 | object: string; 259 | } 260 | 261 | export interface MessageListParams extends CursorPageParams { 262 | order?: 'asc' | 'desc'; 263 | } 264 | -------------------------------------------------------------------------------- /src/apis/collections.ts: -------------------------------------------------------------------------------- 1 | import { COLLECTIONS_API } from '../constants'; 2 | import { ApiClientInterface } from '../_types/generalTypes'; 3 | import { ApiResource } from '../apiResource'; 4 | import { APIPromise, RequestOptions } from '../baseClient'; 5 | import { overrideConfig, toQueryParams } from '../utils'; 6 | import { createHeaders } from './createHeaders'; 7 | 8 | export interface CollectionBody { 9 | name: string; 10 | workspace_id?: string; 11 | parent_collection_id?: string; 12 | } 13 | 14 | export interface CollectionsListQuery { 15 | workspace_id?: string; 16 | current_page?: number; 17 | page_size?: number; 18 | search?: string; 19 | } 20 | 21 | export class Collections extends ApiResource { 22 | create( 23 | _body: CollectionBody, 24 | params?: ApiClientInterface, 25 | opts?: RequestOptions 26 | ): APIPromise { 27 | const body = _body; 28 | if (params) { 29 | const config = overrideConfig(this.client.config, params.config); 30 | this.client.customHeaders = { 31 | ...this.client.customHeaders, 32 | ...createHeaders({ ...params, config }), 33 | }; 34 | } 35 | const response = this.post(`${COLLECTIONS_API}`, { 36 | body, 37 | ...opts, 38 | }); 39 | return response; 40 | } 41 | 42 | list( 43 | _query?: CollectionsListQuery, 44 | params?: ApiClientInterface, 45 | opts?: RequestOptions 46 | ): APIPromise { 47 | const query = _query ? toQueryParams(_query) : ''; 48 | if (params) { 49 | const config = overrideConfig(this.client.config, params.config); 50 | this.client.customHeaders = { 51 | ...this.client.customHeaders, 52 | ...createHeaders({ ...params, config }), 53 | }; 54 | } 55 | const response = this.getMethod(`${COLLECTIONS_API}${query}`, { 56 | ...opts, 57 | }); 58 | return response; 59 | } 60 | 61 | retrieve( 62 | collectionId: string, 63 | params?: ApiClientInterface, 64 | opts?: RequestOptions 65 | ): APIPromise { 66 | if (params) { 67 | const config = overrideConfig(this.client.config, params.config); 68 | this.client.customHeaders = { 69 | ...this.client.customHeaders, 70 | ...createHeaders({ ...params, config }), 71 | }; 72 | } 73 | const response = this.getMethod(`${COLLECTIONS_API}/${collectionId}`, { 74 | ...opts, 75 | }); 76 | return response; 77 | } 78 | 79 | update( 80 | collectionId: string, 81 | body: { 82 | name?: string; 83 | }, 84 | params?: ApiClientInterface, 85 | opts?: RequestOptions 86 | ): APIPromise { 87 | if (params) { 88 | const config = overrideConfig(this.client.config, params.config); 89 | this.client.customHeaders = { 90 | ...this.client.customHeaders, 91 | ...createHeaders({ ...params, config }), 92 | }; 93 | } 94 | const response = this.put(`${COLLECTIONS_API}/${collectionId}`, { 95 | body, 96 | ...opts, 97 | }); 98 | return response; 99 | } 100 | 101 | delete( 102 | collectionId: string, 103 | params?: ApiClientInterface, 104 | opts?: RequestOptions 105 | ): APIPromise { 106 | if (params) { 107 | const config = overrideConfig(this.client.config, params.config); 108 | this.client.customHeaders = { 109 | ...this.client.customHeaders, 110 | ...createHeaders({ ...params, config }), 111 | }; 112 | } 113 | const response = this.deleteMethod( 114 | `${COLLECTIONS_API}/${collectionId}`, 115 | { 116 | ...opts, 117 | } 118 | ); 119 | return response; 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /src/apis/completions.ts: -------------------------------------------------------------------------------- 1 | import { APIResponseType, ApiClientInterface } from '../_types/generalTypes'; 2 | import { ModelParams } from '../_types/portkeyConstructs'; 3 | import { ApiResource } from '../apiResource'; 4 | import { APIPromise, RequestOptions } from '../baseClient'; 5 | import { TEXT_COMPLETE_API } from '../constants'; 6 | import { Stream } from '../streaming'; 7 | import { overrideConfig } from '../utils'; 8 | import { createHeaders } from './createHeaders'; 9 | 10 | export class Completions extends ApiResource { 11 | create( 12 | _body: CompletionsBodyNonStreaming, 13 | params?: ApiClientInterface, 14 | opts?: RequestOptions 15 | ): APIPromise; 16 | create( 17 | _body: CompletionsBodyStreaming, 18 | params?: ApiClientInterface, 19 | opts?: RequestOptions 20 | ): APIPromise>; 21 | create( 22 | _body: CompletionsBodyBase, 23 | params?: ApiClientInterface, 24 | opts?: RequestOptions 25 | ): APIPromise | TextCompletion>; 26 | create( 27 | _body: CompletionCreateParams, 28 | params?: ApiClientInterface, 29 | opts?: RequestOptions 30 | ): APIPromise | APIPromise> { 31 | const body = _body; 32 | // If config is present then override it. 33 | if (params) { 34 | const config = overrideConfig(this.client.config, params.config); 35 | this.client.customHeaders = { 36 | ...this.client.customHeaders, 37 | ...createHeaders({ ...params, config }), 38 | }; 39 | } 40 | const stream = _body.stream ?? false; 41 | 42 | this.client.responseHeaders; 43 | return this.post(TEXT_COMPLETE_API, { body, ...opts, stream }) as 44 | | APIPromise 45 | | APIPromise>; 46 | } 47 | } 48 | 49 | export interface CompletionsBodyBase extends ModelParams { 50 | prompt: string; 51 | } 52 | 53 | export interface ChatCompletionStreamOptions { 54 | include_usage?: boolean; 55 | } 56 | 57 | export interface CompletionsBodyStreaming extends CompletionsBodyBase { 58 | stream?: true; 59 | stream_options?: ChatCompletionStreamOptions | null; 60 | } 61 | 62 | export interface CompletionsBodyNonStreaming extends CompletionsBodyBase { 63 | stream?: false; 64 | } 65 | 66 | export type CompletionCreateParams = 67 | | CompletionsBodyNonStreaming 68 | | CompletionsBodyStreaming; 69 | 70 | interface Usage { 71 | prompt_tokens?: number; 72 | completion_tokens?: number; 73 | total_tokens?: number; 74 | [key: string]: any; 75 | } 76 | 77 | interface Logprobs { 78 | text_offset?: Array; 79 | token_logprobs?: Array; 80 | tokens?: Array; 81 | top_logprobs?: Array>; 82 | [key: string]: any; 83 | } 84 | 85 | interface Choices { 86 | index?: number; 87 | text?: string; 88 | logprobs: Logprobs; 89 | finish_reason?: string; 90 | [key: string]: any; 91 | } 92 | 93 | interface TextCompletion extends APIResponseType { 94 | id: string; 95 | object: string; 96 | created: number; 97 | model: string; 98 | choices: Array; 99 | usage?: Usage; 100 | system_fingerprint?: string; 101 | [key: string]: any; 102 | } 103 | -------------------------------------------------------------------------------- /src/apis/configs.ts: -------------------------------------------------------------------------------- 1 | import { ApiResource } from '../apiResource'; 2 | import { APIResponseType, ApiClientInterface } from '../_types/generalTypes'; 3 | import { APIPromise, RequestOptions } from '../baseClient'; 4 | import { createHeaders } from './createHeaders'; 5 | import { toQueryParams } from '../utils'; 6 | 7 | export interface ConfigsAddParams { 8 | name?: string; 9 | config?: Record; 10 | isDefault?: number; 11 | workspace_id?: string; 12 | } 13 | export interface ConfigsAddResponse extends APIResponseType { 14 | id?: string; 15 | version_id?: string; 16 | slug?: string; 17 | object?: string; 18 | } 19 | export interface ConfigsGetParams { 20 | slug?: string; 21 | } 22 | export interface ConfigsGetResponse extends APIResponseType { 23 | id?: string; 24 | name?: string; 25 | workspace_id?: string; 26 | slug?: string; 27 | organization_id?: string; 28 | is_default?: number; 29 | status?: string; 30 | owner_id?: string; 31 | created_at?: string; 32 | updated_by?: string; 33 | last_updated_at?: string; 34 | config?: Record; 35 | format?: string; 36 | type?: string; 37 | version_id?: string; 38 | object?: string; 39 | } 40 | export interface CongfigsListParams { 41 | workspace_id?: string; 42 | } 43 | export interface ConfigsListResponse extends APIResponseType { 44 | object?: boolean; 45 | total?: number; 46 | data?: Record[]; 47 | } 48 | export interface ConfigsUpdateParams { 49 | slug?: string; 50 | name?: string; 51 | config?: Record; 52 | status?: string; 53 | } 54 | export interface ConfigsUpdateResponse extends APIResponseType { 55 | version_id?: string; 56 | object?: string; 57 | } 58 | export interface ConfigsDeleteParams { 59 | id?: string; 60 | slug?: string; 61 | } 62 | export class Configs extends ApiResource { 63 | constructor(client: any) { 64 | super(client); 65 | } 66 | create( 67 | _body: ConfigsAddParams, 68 | params?: ApiClientInterface, 69 | opts?: RequestOptions 70 | ): APIPromise { 71 | const body = _body; 72 | if (params) { 73 | this.client.customHeaders = { 74 | ...this.client.customHeaders, 75 | ...createHeaders({ ...params }), 76 | }; 77 | } 78 | const response = this.post('/configs', { 79 | body, 80 | ...opts, 81 | }); 82 | return response; 83 | } 84 | 85 | retrieve( 86 | _body: ConfigsGetParams, 87 | params?: ApiClientInterface, 88 | opts?: RequestOptions 89 | ): APIPromise { 90 | const body = _body; 91 | const slug = body.slug; 92 | if (params) { 93 | this.client.customHeaders = { 94 | ...this.client.customHeaders, 95 | ...createHeaders({ ...params }), 96 | }; 97 | } 98 | const response = this.getMethod(`/configs/${slug}`, { 99 | ...opts, 100 | }); 101 | return response; 102 | } 103 | 104 | update( 105 | _body: ConfigsUpdateParams, 106 | params?: ApiClientInterface, 107 | opts?: RequestOptions 108 | ): APIPromise { 109 | const body = _body; 110 | const slug = body.slug; 111 | if (params) { 112 | this.client.customHeaders = { 113 | ...this.client.customHeaders, 114 | ...createHeaders({ ...params }), 115 | }; 116 | } 117 | const response = this.put(`/configs/${slug}`, { 118 | body, 119 | ...opts, 120 | }); 121 | return response; 122 | } 123 | list( 124 | _body?: CongfigsListParams, 125 | params?: ApiClientInterface, 126 | opts?: RequestOptions 127 | ): APIPromise { 128 | const body = _body; 129 | const query = toQueryParams(body); 130 | if (params) { 131 | this.client.customHeaders = { 132 | ...this.client.customHeaders, 133 | ...createHeaders({ ...params }), 134 | }; 135 | } 136 | const response = this.getMethod(`/configs${query}`, { 137 | ...opts, 138 | }); 139 | return response; 140 | } 141 | delete( 142 | _body: ConfigsDeleteParams, 143 | params?: ApiClientInterface, 144 | opts?: RequestOptions 145 | ): APIPromise { 146 | const body = _body; 147 | let configSlug = ''; 148 | if (body.id) { 149 | configSlug = body.id; 150 | /* eslint-disable-next-line no-console */ 151 | console.warn( 152 | 'You are using "id" to delete a config. This will be deprecated in the future. Please use "slug" instead.' 153 | ); 154 | } else if (body.slug) { 155 | configSlug = body.slug; 156 | } 157 | if (params) { 158 | this.client.customHeaders = { 159 | ...this.client.customHeaders, 160 | ...createHeaders({ ...params }), 161 | }; 162 | } 163 | const response = this.deleteMethod(`/configs/${configSlug}`, { 164 | ...opts, 165 | }); 166 | return response; 167 | } 168 | } 169 | -------------------------------------------------------------------------------- /src/apis/createHeaders.ts: -------------------------------------------------------------------------------- 1 | import { getPortkeyHeader, isEmpty } from '../utils'; 2 | 3 | export const createHeaders = ( 4 | config: Record 5 | ): Record => { 6 | const headers: Record = {}; 7 | let forwardHeaders: string[] = []; 8 | 9 | if (config['forwardHeaders']) { 10 | // logic to convert forwardHeaders values to kebab-case 11 | forwardHeaders = config['forwardHeaders'].map((header: string) => { 12 | return header 13 | .replace('ID', 'Id') 14 | .replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`); 15 | }); 16 | } 17 | 18 | for (let k in config) { 19 | let v = config[k]; 20 | if (isEmpty(v)) continue; 21 | 22 | // convert to snakecase 23 | if (k.toLocaleLowerCase() === 'authorization') { 24 | headers[k.toLowerCase()] = v || ''; 25 | continue; 26 | } 27 | 28 | // false logic (type is boolean, to handle flasy logic) 29 | if (typeof v === 'boolean') { 30 | v = v.toString(); 31 | } 32 | 33 | // logic to handle forwardHeaders into a comma separated string 34 | if (k === 'forwardHeaders') { 35 | v = v.join(','); 36 | } 37 | 38 | k = k 39 | .replace('ID', 'Id') 40 | .replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`); 41 | if (!isEmpty(v) && typeof v == 'object') { 42 | v = JSON.stringify(v); 43 | } 44 | if (forwardHeaders && forwardHeaders.includes(k)) { 45 | headers[k] = v || ''; 46 | } else { 47 | headers[getPortkeyHeader(k)] = v || ''; 48 | } 49 | } 50 | return headers; 51 | }; 52 | -------------------------------------------------------------------------------- /src/apis/deleteMethod.ts: -------------------------------------------------------------------------------- 1 | import { APIResponseType, ApiClientInterface } from '../_types/generalTypes'; 2 | import { ApiResource } from '../apiResource'; 3 | import { APIPromise, RequestOptions } from '../baseClient'; 4 | import { overrideConfig } from '../utils'; 5 | import { createHeaders } from './createHeaders'; 6 | 7 | export class deleteMethod extends ApiResource { 8 | create( 9 | path: string, 10 | params?: ApiClientInterface, 11 | opts?: RequestOptions 12 | ): APIPromise { 13 | if (params) { 14 | const config = overrideConfig(this.client.config, params.config); 15 | this.client.customHeaders = { 16 | ...this.client.customHeaders, 17 | ...createHeaders({ ...params, config }), 18 | }; 19 | } 20 | const response = this.deleteMethod(path, { ...opts }); 21 | return response; 22 | } 23 | } 24 | 25 | export type DeleteResponse = Record & APIResponseType; 26 | -------------------------------------------------------------------------------- /src/apis/embeddings.ts: -------------------------------------------------------------------------------- 1 | import { APIResponseType, ApiClientInterface } from '../_types/generalTypes'; 2 | import { ModelParams } from '../_types/portkeyConstructs'; 3 | import { ApiResource } from '../apiResource'; 4 | import { APIPromise, RequestOptions } from '../baseClient'; 5 | import { EMBEDDINGS_API } from '../constants'; 6 | import { overrideConfig } from '../utils'; 7 | import { createHeaders } from './createHeaders'; 8 | 9 | export interface EmbeddingsBody extends ModelParams { 10 | input: string | number | (string | number)[] | (string | number)[][]; 11 | model?: string; 12 | dimensions?: number; 13 | encoding_format?: string; 14 | } 15 | 16 | export interface EmbeddingArr { 17 | embedding?: Array; 18 | index?: number; 19 | object?: string; 20 | [key: string]: any; 21 | } 22 | 23 | export interface Usage { 24 | prompt_tokens?: number; 25 | total_tokens?: number; 26 | [key: string]: any; 27 | } 28 | 29 | interface EmbeddingsResponse extends APIResponseType { 30 | data?: Array; 31 | model?: string; 32 | object?: string; 33 | usage?: Usage; 34 | [key: string]: any; 35 | } 36 | 37 | export class Embeddings extends ApiResource { 38 | create( 39 | _body: EmbeddingsBody, 40 | params?: ApiClientInterface, 41 | opts?: RequestOptions 42 | ): APIPromise { 43 | const body = _body; 44 | if (params) { 45 | const config = overrideConfig(this.client.config, params.config); 46 | this.client.customHeaders = { 47 | ...this.client.customHeaders, 48 | ...createHeaders({ ...params, config }), 49 | }; 50 | } 51 | const response = this.post(EMBEDDINGS_API, { 52 | body, 53 | ...opts, 54 | }); 55 | return response; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/apis/evals.ts: -------------------------------------------------------------------------------- 1 | import { 2 | EvalCreateParams, 3 | EvalCreateResponse, 4 | EvalListParams, 5 | EvalRetrieveResponse, 6 | EvalUpdateParams, 7 | EvalUpdateResponse, 8 | } from 'openai/resources/evals/evals'; 9 | import { ApiResource } from '../apiResource'; 10 | import { ApiClientInterface } from '../_types/generalTypes'; 11 | import { RequestOptions } from '../baseClient'; 12 | import { finalResponse, initOpenAIClient, overrideConfig } from '../utils'; 13 | import { createHeaders } from './createHeaders'; 14 | import { 15 | RunCreateParams, 16 | RunCreateResponse, 17 | RunListParams, 18 | RunRetrieveResponse, 19 | } from 'openai/resources/evals/runs/runs'; 20 | import { OutputItemListParams } from 'openai/resources/evals/runs/output-items'; 21 | 22 | export class Evals extends ApiResource { 23 | runs: EvalsRuns; 24 | constructor(client: any) { 25 | super(client); 26 | this.runs = new EvalsRuns(client); 27 | } 28 | 29 | async create( 30 | body: EvalCreateParams, 31 | params?: ApiClientInterface, 32 | opts?: RequestOptions 33 | ): Promise { 34 | if (params) { 35 | const config = overrideConfig(this.client.config, params.config); 36 | this.client.customHeaders = { 37 | ...this.client.customHeaders, 38 | ...createHeaders({ ...params, config }), 39 | }; 40 | } 41 | const OAIclient = initOpenAIClient(this.client); 42 | const result = await OAIclient.evals.create(body, opts); 43 | return result; 44 | } 45 | 46 | async retrieve( 47 | evalId: string, 48 | params?: ApiClientInterface, 49 | opts?: RequestOptions 50 | ): Promise { 51 | if (params) { 52 | const config = overrideConfig(this.client.config, params.config); 53 | this.client.customHeaders = { 54 | ...this.client.customHeaders, 55 | ...createHeaders({ ...params, config }), 56 | }; 57 | } 58 | const OAIclient = initOpenAIClient(this.client); 59 | const result = await OAIclient.evals.retrieve(evalId, opts).withResponse(); 60 | return finalResponse(result); 61 | } 62 | 63 | async update( 64 | evalId: string, 65 | body: EvalUpdateParams, 66 | params?: ApiClientInterface, 67 | opts?: RequestOptions 68 | ): Promise { 69 | if (params) { 70 | const config = overrideConfig(this.client.config, params.config); 71 | this.client.customHeaders = { 72 | ...this.client.customHeaders, 73 | ...createHeaders({ ...params, config }), 74 | }; 75 | } 76 | const OAIclient = initOpenAIClient(this.client); 77 | const result = await OAIclient.evals 78 | .update(evalId, body, opts) 79 | .withResponse(); 80 | return finalResponse(result); 81 | } 82 | 83 | async list( 84 | query?: EvalListParams, 85 | params?: ApiClientInterface, 86 | opts?: RequestOptions 87 | ): Promise { 88 | if (params) { 89 | const config = overrideConfig(this.client.config, params.config); 90 | this.client.customHeaders = { 91 | ...this.client.customHeaders, 92 | ...createHeaders({ ...params, config }), 93 | }; 94 | } 95 | const OAIclient = initOpenAIClient(this.client); 96 | const result = await OAIclient.evals.list(query, opts).withResponse(); 97 | return finalResponse(result); 98 | } 99 | 100 | async del( 101 | evalId: string, 102 | params?: ApiClientInterface, 103 | opts?: RequestOptions 104 | ): Promise { 105 | if (params) { 106 | const config = overrideConfig(this.client.config, params.config); 107 | this.client.customHeaders = { 108 | ...this.client.customHeaders, 109 | ...createHeaders({ ...params, config }), 110 | }; 111 | } 112 | const OAIclient = initOpenAIClient(this.client); 113 | const result = await OAIclient.evals.del(evalId, opts).withResponse(); 114 | return finalResponse(result); 115 | } 116 | } 117 | 118 | export class EvalsRuns extends ApiResource { 119 | outputItems: OutputItems; 120 | constructor(client: any) { 121 | super(client); 122 | this.outputItems = new OutputItems(client); 123 | } 124 | 125 | async create( 126 | evalId: string, 127 | body: RunCreateParams, 128 | params?: ApiClientInterface, 129 | opts?: RequestOptions 130 | ): Promise { 131 | if (params) { 132 | const config = overrideConfig(this.client.config, params.config); 133 | this.client.customHeaders = { 134 | ...this.client.customHeaders, 135 | ...createHeaders({ ...params, config }), 136 | }; 137 | } 138 | const OAIclient = initOpenAIClient(this.client); 139 | const result = await OAIclient.evals.runs 140 | .create(evalId, body, opts) 141 | .withResponse(); 142 | return finalResponse(result); 143 | } 144 | 145 | async retrieve( 146 | evalId: string, 147 | runId: string, 148 | params?: ApiClientInterface, 149 | opts?: RequestOptions 150 | ): Promise { 151 | if (params) { 152 | const config = overrideConfig(this.client.config, params.config); 153 | this.client.customHeaders = { 154 | ...this.client.customHeaders, 155 | ...createHeaders({ ...params, config }), 156 | }; 157 | } 158 | const OAIclient = initOpenAIClient(this.client); 159 | const result = await OAIclient.evals.runs 160 | .retrieve(evalId, runId, opts) 161 | .withResponse(); 162 | return finalResponse(result); 163 | } 164 | 165 | async list( 166 | evalId: string, 167 | query?: RunListParams, 168 | params?: ApiClientInterface, 169 | opts?: RequestOptions 170 | ): Promise { 171 | if (params) { 172 | const config = overrideConfig(this.client.config, params.config); 173 | this.client.customHeaders = { 174 | ...this.client.customHeaders, 175 | ...createHeaders({ ...params, config }), 176 | }; 177 | } 178 | const OAIclient = initOpenAIClient(this.client); 179 | const result = await OAIclient.evals.runs 180 | .list(evalId, query, opts) 181 | .withResponse(); 182 | return finalResponse(result); 183 | } 184 | 185 | async del( 186 | evalId: string, 187 | runId: string, 188 | params?: ApiClientInterface, 189 | opts?: RequestOptions 190 | ): Promise { 191 | if (params) { 192 | const config = overrideConfig(this.client.config, params.config); 193 | this.client.customHeaders = { 194 | ...this.client.customHeaders, 195 | ...createHeaders({ ...params, config }), 196 | }; 197 | } 198 | const OAIclient = initOpenAIClient(this.client); 199 | const result = await OAIclient.evals.runs 200 | .del(evalId, runId, opts) 201 | .withResponse(); 202 | return finalResponse(result); 203 | } 204 | 205 | async cancel( 206 | evalId: string, 207 | runId: string, 208 | params?: ApiClientInterface, 209 | opts?: RequestOptions 210 | ): Promise { 211 | if (params) { 212 | const config = overrideConfig(this.client.config, params.config); 213 | this.client.customHeaders = { 214 | ...this.client.customHeaders, 215 | ...createHeaders({ ...params, config }), 216 | }; 217 | } 218 | const OAIclient = initOpenAIClient(this.client); 219 | const result = await OAIclient.evals.runs 220 | .cancel(evalId, runId, opts) 221 | .withResponse(); 222 | return finalResponse(result); 223 | } 224 | } 225 | 226 | export class OutputItems extends ApiResource { 227 | async retrieve( 228 | evalId: string, 229 | runId: string, 230 | outputItemId: string, 231 | params?: ApiClientInterface, 232 | opts?: RequestOptions 233 | ): Promise { 234 | if (params) { 235 | const config = overrideConfig(this.client.config, params.config); 236 | this.client.customHeaders = { 237 | ...this.client.customHeaders, 238 | ...createHeaders({ ...params, config }), 239 | }; 240 | } 241 | const OAIclient = initOpenAIClient(this.client); 242 | const result = await OAIclient.evals.runs.outputItems 243 | .retrieve(evalId, runId, outputItemId, opts) 244 | .withResponse(); 245 | return finalResponse(result); 246 | } 247 | 248 | async list( 249 | evalId: string, 250 | runId: string, 251 | query?: OutputItemListParams, 252 | params?: ApiClientInterface, 253 | opts?: RequestOptions 254 | ): Promise { 255 | if (params) { 256 | const config = overrideConfig(this.client.config, params.config); 257 | this.client.customHeaders = { 258 | ...this.client.customHeaders, 259 | ...createHeaders({ ...params, config }), 260 | }; 261 | } 262 | const OAIclient = initOpenAIClient(this.client); 263 | const result = await OAIclient.evals.runs.outputItems 264 | .list(evalId, runId, query, opts) 265 | .withResponse(); 266 | return finalResponse(result); 267 | } 268 | } 269 | -------------------------------------------------------------------------------- /src/apis/feedback.ts: -------------------------------------------------------------------------------- 1 | import { APIResponseType, ApiClientInterface } from '../_types/generalTypes'; 2 | import { ApiResource } from '../apiResource'; 3 | import { APIPromise, RequestOptions } from '../baseClient'; 4 | import { FEEDBACK_API } from '../constants'; 5 | import { overrideConfig } from '../utils'; 6 | import { createHeaders } from './createHeaders'; 7 | 8 | interface FeedbackBodyBase { 9 | traceID?: string; 10 | feedbackID?: string; 11 | value?: number; 12 | weight?: number; 13 | metadata?: Record; 14 | } 15 | 16 | type FeedbackBody = FeedbackBodyBase | Array; 17 | 18 | export interface FeedbackResponse extends APIResponseType { 19 | status: string; 20 | message: string; 21 | feedback_id: Array; 22 | } 23 | 24 | export class Feedback extends ApiResource { 25 | create( 26 | _body: FeedbackBody, 27 | params?: ApiClientInterface, 28 | opts?: RequestOptions 29 | ): APIPromise { 30 | const body = _body; 31 | if (params) { 32 | const config = overrideConfig(this.client.config, params.config); 33 | this.client.customHeaders = { 34 | ...this.client.customHeaders, 35 | ...createHeaders({ ...params, config }), 36 | }; 37 | } 38 | const response = this.post(FEEDBACK_API, { 39 | body, 40 | ...opts, 41 | }); 42 | return response; 43 | } 44 | 45 | update( 46 | _body: FeedbackBodyBase, 47 | params?: ApiClientInterface, 48 | opts?: RequestOptions 49 | ): APIPromise { 50 | const body = _body; 51 | const feedbackID = _body.feedbackID; 52 | if (params) { 53 | const config = overrideConfig(this.client.config, params.config); 54 | this.client.customHeaders = { 55 | ...this.client.customHeaders, 56 | ...createHeaders({ ...params, config }), 57 | }; 58 | } 59 | const response = this.put( 60 | FEEDBACK_API + '/' + feedbackID, 61 | { body, ...opts } 62 | ); 63 | return response; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/apis/files.ts: -------------------------------------------------------------------------------- 1 | import { ApiClientInterface } from '../_types/generalTypes'; 2 | import { ApiResource } from '../apiResource'; 3 | import { RequestOptions } from '../baseClient'; 4 | import { finalResponse, initOpenAIClient, overrideConfig } from '../utils'; 5 | import { createHeaders } from './createHeaders'; 6 | 7 | export class MainFiles extends ApiResource { 8 | async create( 9 | _body: FileCreateParams, 10 | params?: ApiClientInterface, 11 | opts?: RequestOptions 12 | ): Promise { 13 | const body: FileCreateParams = _body; 14 | if (params) { 15 | const config = overrideConfig(this.client.config, params.config); 16 | this.client.customHeaders = { 17 | ...this.client.customHeaders, 18 | ...createHeaders({ ...params, config }), 19 | }; 20 | } 21 | 22 | const OAIclient = initOpenAIClient(this.client); 23 | 24 | // eslint-disable-next-line @typescript-eslint/ban-ts-comment 25 | // @ts-ignore 26 | const result = await OAIclient.files.create(body, opts).withResponse(); 27 | 28 | return finalResponse(result); 29 | } 30 | 31 | async list( 32 | _query?: FileListParams, 33 | params?: ApiClientInterface, 34 | opts?: RequestOptions 35 | ): Promise { 36 | const query: FileListParams | undefined = _query; 37 | if (params) { 38 | const config = overrideConfig(this.client.config, params.config); 39 | this.client.customHeaders = { 40 | ...this.client.customHeaders, 41 | ...createHeaders({ ...params, config }), 42 | }; 43 | } 44 | 45 | const OAIclient = initOpenAIClient(this.client); 46 | 47 | const result = await OAIclient.files.list(query, opts).withResponse(); 48 | 49 | return finalResponse(result); 50 | } 51 | 52 | async retrieve( 53 | fileId: string, 54 | params?: ApiClientInterface, 55 | opts?: RequestOptions 56 | ): Promise { 57 | if (params) { 58 | const config = overrideConfig(this.client.config, params.config); 59 | this.client.customHeaders = { 60 | ...this.client.customHeaders, 61 | ...createHeaders({ ...params, config }), 62 | }; 63 | } 64 | 65 | const OAIclient = initOpenAIClient(this.client); 66 | 67 | const result = await OAIclient.files.retrieve(fileId, opts).withResponse(); 68 | 69 | return finalResponse(result); 70 | } 71 | 72 | async del( 73 | fileId: string, 74 | params?: ApiClientInterface, 75 | opts?: RequestOptions 76 | ): Promise { 77 | if (params) { 78 | const config = overrideConfig(this.client.config, params.config); 79 | this.client.customHeaders = { 80 | ...this.client.customHeaders, 81 | ...createHeaders({ ...params, config }), 82 | }; 83 | } 84 | 85 | const OAIclient = initOpenAIClient(this.client); 86 | 87 | const result = await OAIclient.files.del(fileId, opts).withResponse(); 88 | 89 | return finalResponse(result); 90 | } 91 | 92 | async content( 93 | fileId: string, 94 | params?: ApiClientInterface, 95 | opts?: RequestOptions 96 | ): Promise { 97 | if (params) { 98 | const config = overrideConfig(this.client.config, params.config); 99 | this.client.customHeaders = { 100 | ...this.client.customHeaders, 101 | ...createHeaders({ ...params, config }), 102 | }; 103 | } 104 | 105 | const OAIclient = initOpenAIClient(this.client); 106 | 107 | const result = await OAIclient.files.content(fileId, opts).withResponse(); 108 | 109 | return finalResponse(result); 110 | } 111 | 112 | async retrieveContent( 113 | fileId: string, 114 | params?: ApiClientInterface, 115 | opts?: RequestOptions 116 | ): Promise { 117 | if (params) { 118 | const config = overrideConfig(this.client.config, params.config); 119 | this.client.customHeaders = { 120 | ...this.client.customHeaders, 121 | ...createHeaders({ ...params, config }), 122 | }; 123 | } 124 | 125 | const OAIclient = initOpenAIClient(this.client); 126 | 127 | const result = await OAIclient.files.content(fileId, opts).withResponse(); 128 | 129 | return finalResponse(result); 130 | } 131 | 132 | async waitForProcessing( 133 | id: string, 134 | _body: PollOptions, 135 | params?: ApiClientInterface 136 | ): Promise { 137 | const body: PollOptions = _body; 138 | if (params) { 139 | const config = overrideConfig(this.client.config, params.config); 140 | this.client.customHeaders = { 141 | ...this.client.customHeaders, 142 | ...createHeaders({ ...params, config }), 143 | }; 144 | } 145 | 146 | const OAIclient = initOpenAIClient(this.client); 147 | 148 | const result = await OAIclient.files.waitForProcessing(id, { 149 | ...(body.pollInterval !== undefined && { 150 | pollInterval: body.pollInterval, 151 | }), 152 | ...(body.maxWait !== undefined && { maxWait: body.maxWait }), 153 | }); 154 | 155 | return result; 156 | } 157 | } 158 | 159 | interface PollOptions { 160 | pollInterval?: number | undefined; 161 | maxWait?: number | undefined; 162 | } 163 | 164 | export interface FileCreateParams { 165 | file: any; 166 | purpose?: string; 167 | [key: string]: any; 168 | } 169 | 170 | export interface FileObject { 171 | id: string; 172 | bytes?: number; 173 | created_at?: number; 174 | filename?: string; 175 | object?: string; 176 | purpose?: string; 177 | status?: string; 178 | status_details?: string; 179 | [key: string]: any; 180 | } 181 | 182 | export interface FileListParams { 183 | purpose?: string; 184 | order?: 'asc' | 'desc'; 185 | [key: string]: any; 186 | } 187 | -------------------------------------------------------------------------------- /src/apis/fineTuning.ts: -------------------------------------------------------------------------------- 1 | import { 2 | JobCreateParams, 3 | JobListEventsParams, 4 | JobListParams, 5 | } from 'openai/resources/fine-tuning/jobs/jobs'; 6 | import { ApiClientInterface } from '../_types/generalTypes'; 7 | import { ApiResource } from '../apiResource'; 8 | import { RequestOptions } from '../baseClient'; 9 | import { finalResponse, initOpenAIClient, overrideConfig } from '../utils'; 10 | import { createHeaders } from './createHeaders'; 11 | import { CheckpointListParams } from 'openai/resources/fine-tuning/jobs/checkpoints'; 12 | import { 13 | PermissionCreateParams, 14 | PermissionRetrieveParams, 15 | } from 'openai/resources/fine-tuning/checkpoints/permissions'; 16 | import { 17 | GraderRunParams, 18 | GraderValidateParams, 19 | } from 'openai/resources/fine-tuning/alpha/graders'; 20 | 21 | export class FineTuning extends ApiResource { 22 | jobs: Jobs; 23 | checkpoints: FineTuningCheckpoints; 24 | alpha: Alpha; 25 | constructor(client: any) { 26 | super(client); 27 | this.jobs = new Jobs(client); 28 | this.checkpoints = new FineTuningCheckpoints(client); 29 | this.alpha = new Alpha(client); 30 | } 31 | } 32 | 33 | export class Jobs extends ApiResource { 34 | checkpoints: Checkpoints; 35 | constructor(client: any) { 36 | super(client); 37 | this.checkpoints = new Checkpoints(client); 38 | } 39 | 40 | async create( 41 | _body: JobCreateBody, 42 | params?: ApiClientInterface, 43 | opts?: RequestOptions 44 | ): Promise { 45 | const body: JobCreateBody = _body; 46 | if (params) { 47 | const config = overrideConfig(this.client.config, params.config); 48 | this.client.customHeaders = { 49 | ...this.client.customHeaders, 50 | ...createHeaders({ ...params, config }), 51 | }; 52 | } 53 | 54 | const OAIclient = initOpenAIClient(this.client); 55 | 56 | const result = await OAIclient.fineTuning.jobs 57 | .create(body, opts) 58 | .withResponse(); 59 | return finalResponse(result); 60 | } 61 | 62 | async retrieve( 63 | fineTuningJobId: string, 64 | params?: ApiClientInterface, 65 | opts?: RequestOptions 66 | ): Promise { 67 | if (params) { 68 | const config = overrideConfig(this.client.config, params.config); 69 | this.client.customHeaders = { 70 | ...this.client.customHeaders, 71 | ...createHeaders({ ...params, config }), 72 | }; 73 | } 74 | 75 | const OAIclient = initOpenAIClient(this.client); 76 | 77 | const result = await OAIclient.fineTuning.jobs 78 | .retrieve(fineTuningJobId, opts) 79 | .withResponse(); 80 | return finalResponse(result); 81 | } 82 | 83 | async list( 84 | _query?: JobListParams, 85 | params?: ApiClientInterface, 86 | opts?: RequestOptions 87 | ): Promise { 88 | const query: JobListParams | undefined = _query; 89 | if (params) { 90 | const config = overrideConfig(this.client.config, params.config); 91 | this.client.customHeaders = { 92 | ...this.client.customHeaders, 93 | ...createHeaders({ ...params, config }), 94 | }; 95 | } 96 | 97 | const OAIclient = initOpenAIClient(this.client); 98 | 99 | const result = await OAIclient.fineTuning.jobs 100 | .list(query, opts) 101 | .withResponse(); 102 | return finalResponse(result); 103 | } 104 | 105 | async cancel( 106 | fineTuningJobId: string, 107 | params?: ApiClientInterface, 108 | opts?: RequestOptions 109 | ): Promise { 110 | if (params) { 111 | const config = overrideConfig(this.client.config, params.config); 112 | this.client.customHeaders = { 113 | ...this.client.customHeaders, 114 | ...createHeaders({ ...params, config }), 115 | }; 116 | } 117 | 118 | const OAIclient = initOpenAIClient(this.client); 119 | const body = {}; 120 | const options = { body, ...opts }; 121 | 122 | const result = await OAIclient.fineTuning.jobs 123 | .cancel(fineTuningJobId, options) 124 | .withResponse(); 125 | return finalResponse(result); 126 | } 127 | 128 | async listEvents( 129 | fineTuningJobId: string, 130 | _query?: JobListEventsParams, 131 | params?: ApiClientInterface, 132 | opts?: RequestOptions 133 | ): Promise { 134 | const query: JobListEventsParams | undefined = _query; 135 | if (params) { 136 | const config = overrideConfig(this.client.config, params.config); 137 | this.client.customHeaders = { 138 | ...this.client.customHeaders, 139 | ...createHeaders({ ...params, config }), 140 | }; 141 | } 142 | 143 | const OAIclient = initOpenAIClient(this.client); 144 | 145 | const result = await OAIclient.fineTuning.jobs 146 | .listEvents(fineTuningJobId, query, opts) 147 | .withResponse(); 148 | return finalResponse(result); 149 | } 150 | } 151 | 152 | export class Checkpoints extends ApiResource { 153 | async list( 154 | fineTuningJobId: string, 155 | _query?: CheckpointListParams, 156 | params?: ApiClientInterface, 157 | opts?: RequestOptions 158 | ): Promise { 159 | const query: CheckpointListParams | undefined = _query; 160 | if (params) { 161 | const config = overrideConfig(this.client.config, params.config); 162 | this.client.customHeaders = { 163 | ...this.client.customHeaders, 164 | ...createHeaders({ ...params, config }), 165 | }; 166 | } 167 | 168 | const OAIclient = initOpenAIClient(this.client); 169 | 170 | const result = await OAIclient.fineTuning.jobs.checkpoints 171 | .list(fineTuningJobId, query, opts) 172 | .withResponse(); 173 | return finalResponse(result); 174 | } 175 | } 176 | 177 | export class Alpha extends ApiResource { 178 | grader: Grader; 179 | constructor(client: any) { 180 | super(client); 181 | this.grader = new Grader(client); 182 | } 183 | } 184 | 185 | export class Grader extends ApiResource { 186 | async run( 187 | body: GraderRunParams, 188 | params?: ApiClientInterface, 189 | opts?: RequestOptions 190 | ): Promise { 191 | if (params) { 192 | const config = overrideConfig(this.client.config, params.config); 193 | this.client.customHeaders = { 194 | ...this.client.customHeaders, 195 | ...createHeaders({ ...params, config }), 196 | }; 197 | } 198 | const OAIclient = initOpenAIClient(this.client); 199 | const result = await OAIclient.fineTuning.alpha.graders 200 | .run(body, opts) 201 | .withResponse(); 202 | return finalResponse(result); 203 | } 204 | 205 | async validate( 206 | body: GraderValidateParams, 207 | params?: ApiClientInterface, 208 | opts?: RequestOptions 209 | ): Promise { 210 | if (params) { 211 | const config = overrideConfig(this.client.config, params.config); 212 | this.client.customHeaders = { 213 | ...this.client.customHeaders, 214 | ...createHeaders({ ...params, config }), 215 | }; 216 | } 217 | 218 | const OAIclient = initOpenAIClient(this.client); 219 | const result = await OAIclient.fineTuning.alpha.graders 220 | .validate(body, opts) 221 | .withResponse(); 222 | return finalResponse(result); 223 | } 224 | } 225 | 226 | export interface JobCreateBody extends JobCreateParams { 227 | role_arn: string; 228 | job_name: string; 229 | output_file: string; 230 | provider_options: Record; 231 | portkey_options: Record; 232 | [key: string]: any; 233 | } 234 | 235 | export class FineTuningCheckpoints extends ApiResource { 236 | permissions: Permissions; 237 | 238 | constructor(client: any) { 239 | super(client); 240 | this.permissions = new Permissions(client); 241 | } 242 | } 243 | 244 | export class Permissions extends ApiResource { 245 | async create( 246 | fineTunedModelCheckpoint: string, 247 | body: PermissionCreateParams, 248 | params?: ApiClientInterface, 249 | opts?: RequestOptions 250 | ): Promise { 251 | if (params) { 252 | const config = overrideConfig(this.client.config, params.config); 253 | this.client.customHeaders = { 254 | ...this.client.customHeaders, 255 | ...createHeaders({ ...params, config }), 256 | }; 257 | } 258 | const OAIclient = initOpenAIClient(this.client); 259 | const result = await OAIclient.fineTuning.checkpoints.permissions 260 | .create(fineTunedModelCheckpoint, body, opts) 261 | .withResponse(); 262 | return finalResponse(result); 263 | } 264 | 265 | async retrieve( 266 | fineTunedModelCheckpoint: string, 267 | query?: PermissionRetrieveParams, 268 | params?: ApiClientInterface, 269 | opts?: RequestOptions 270 | ): Promise { 271 | if (params) { 272 | const config = overrideConfig(this.client.config, params.config); 273 | this.client.customHeaders = { 274 | ...this.client.customHeaders, 275 | ...createHeaders({ ...params, config }), 276 | }; 277 | } 278 | const OAIclient = initOpenAIClient(this.client); 279 | const result = await OAIclient.fineTuning.checkpoints.permissions 280 | .retrieve(fineTunedModelCheckpoint, query, opts) 281 | .withResponse(); 282 | return finalResponse(result); 283 | } 284 | 285 | async del( 286 | fineTunedModelCheckpoint: string, 287 | permissionId: string, 288 | params?: ApiClientInterface, 289 | opts?: RequestOptions 290 | ): Promise { 291 | if (params) { 292 | const config = overrideConfig(this.client.config, params.config); 293 | this.client.customHeaders = { 294 | ...this.client.customHeaders, 295 | ...createHeaders({ ...params, config }), 296 | }; 297 | } 298 | const OAIclient = initOpenAIClient(this.client); 299 | const result = await OAIclient.fineTuning.checkpoints.permissions 300 | .del(fineTunedModelCheckpoint, permissionId, opts) 301 | .withResponse(); 302 | return finalResponse(result); 303 | } 304 | } 305 | -------------------------------------------------------------------------------- /src/apis/getMethod.ts: -------------------------------------------------------------------------------- 1 | import { APIResponseType, ApiClientInterface } from '../_types/generalTypes'; 2 | import { ApiResource } from '../apiResource'; 3 | import { APIPromise, RequestOptions } from '../baseClient'; 4 | import { overrideConfig } from '../utils'; 5 | import { createHeaders } from './createHeaders'; 6 | 7 | export class getMethod extends ApiResource { 8 | create( 9 | path: string, 10 | params?: ApiClientInterface, 11 | opts?: RequestOptions 12 | ): APIPromise { 13 | if (params) { 14 | const config = overrideConfig(this.client.config, params.config); 15 | this.client.customHeaders = { 16 | ...this.client.customHeaders, 17 | ...createHeaders({ ...params, config }), 18 | }; 19 | } 20 | const response = this.getMethod(path, { ...opts }); 21 | return response; 22 | } 23 | } 24 | 25 | export type GetResponse = Record & APIResponseType; 26 | -------------------------------------------------------------------------------- /src/apis/images.ts: -------------------------------------------------------------------------------- 1 | import { ApiClientInterface } from '../_types/generalTypes'; 2 | import { ApiResource } from '../apiResource'; 3 | import { RequestOptions } from '../baseClient'; 4 | import { finalResponse, initOpenAIClient, overrideConfig } from '../utils'; 5 | import { createHeaders } from './createHeaders'; 6 | 7 | export interface ImagesBody { 8 | prompt: string; 9 | model?: string; 10 | n?: number | null; 11 | quality?: string; 12 | response_format?: string | null; 13 | size?: string | null; 14 | style?: string | null; 15 | user?: string; 16 | [key: string]: any; 17 | } 18 | 19 | export interface ImageEditParams { 20 | image: any; 21 | prompt: string; 22 | mask?: any; 23 | model?: string | null; 24 | n?: number | null; 25 | response_format?: string | null; 26 | size?: string | null; 27 | user?: string; 28 | [key: string]: any; 29 | } 30 | 31 | export interface ImageCreateVariationParams { 32 | image: any; 33 | model?: string | null; 34 | n?: number | null; 35 | response_format?: string | null; 36 | size?: string | null; 37 | user?: string; 38 | [key: string]: any; 39 | } 40 | 41 | export interface ImagesResponse { 42 | created: number; 43 | data: Array; 44 | [key: string]: any; 45 | } 46 | 47 | export interface Image { 48 | b64_json?: string; 49 | revised_prompt?: string; 50 | url?: string; 51 | [key: string]: any; 52 | } 53 | 54 | export class Images extends ApiResource { 55 | async generate( 56 | _body: ImagesBody, 57 | params?: ApiClientInterface, 58 | opts?: RequestOptions 59 | ): Promise { 60 | const body: ImagesBody = _body; 61 | if (params) { 62 | const config = overrideConfig(this.client.config, params.config); 63 | this.client.customHeaders = { 64 | ...this.client.customHeaders, 65 | ...createHeaders({ ...params, config }), 66 | }; 67 | } 68 | 69 | const OAIclient = initOpenAIClient(this.client); 70 | 71 | // eslint-disable-next-line @typescript-eslint/ban-ts-comment 72 | // @ts-ignore 73 | const result = await OAIclient.images.generate(body, opts).withResponse(); 74 | 75 | return finalResponse(result); 76 | } 77 | 78 | async edit( 79 | _body: ImageEditParams, 80 | params?: ApiClientInterface, 81 | opts?: RequestOptions 82 | ): Promise { 83 | const body: ImageEditParams = _body; 84 | if (params) { 85 | const config = overrideConfig(this.client.config, params.config); 86 | this.client.customHeaders = { 87 | ...this.client.customHeaders, 88 | ...createHeaders({ ...params, config }), 89 | }; 90 | } 91 | 92 | const OAIclient = initOpenAIClient(this.client); 93 | 94 | // eslint-disable-next-line @typescript-eslint/ban-ts-comment 95 | // @ts-ignore 96 | const result = await OAIclient.images.edit(body, opts).withResponse(); 97 | 98 | return finalResponse(result); 99 | } 100 | 101 | async createVariation( 102 | _body: ImageCreateVariationParams, 103 | params?: ApiClientInterface, 104 | opts?: RequestOptions 105 | ): Promise { 106 | const body: ImageCreateVariationParams = _body; 107 | if (params) { 108 | const config = overrideConfig(this.client.config, params.config); 109 | this.client.customHeaders = { 110 | ...this.client.customHeaders, 111 | ...createHeaders({ ...params, config }), 112 | }; 113 | } 114 | 115 | const OAIclient = initOpenAIClient(this.client); 116 | // eslint-disable-next-line @typescript-eslint/ban-ts-comment 117 | // @ts-ignore 118 | const result = await OAIclient.images 119 | .createVariation(body as any, opts) 120 | .withResponse(); 121 | 122 | return finalResponse(result); 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /src/apis/index.ts: -------------------------------------------------------------------------------- 1 | export { Chat } from './chatCompletions'; 2 | export { Completions } from './completions'; 3 | export { createHeaders } from './createHeaders'; 4 | export { Feedback } from './feedback'; 5 | export { Generations, Prompt } from './generations'; 6 | export { postMethod } from './postMethod'; 7 | export { getMethod } from './getMethod'; 8 | export { deleteMethod } from './deleteMethod'; 9 | export { putMethod } from './putMethod'; 10 | export { Embeddings } from './embeddings'; 11 | export { Images } from './images'; 12 | export { Assistants } from './assistants'; 13 | export { Threads } from './threads'; 14 | export { MainFiles } from './files'; 15 | export { Models } from './models'; 16 | export { Admin } from './admin'; 17 | export { Configs } from './configs'; 18 | export { Batches } from './batches'; 19 | export { VirtualKeys } from './virtualKeys'; 20 | export { FineTuning } from './fineTuning'; 21 | export { Moderations } from './moderations'; 22 | export { Audio } from './audio'; 23 | export { VectorStores } from './vectorStores'; 24 | export { BetaChat } from './betaChat'; 25 | export { Uploads } from './uploads'; 26 | export { ApiKeys } from './apiKeys'; 27 | export { Logs } from './logsExport'; 28 | export { Realtime } from './realtime'; 29 | export { Responses } from './responses'; 30 | export { Labels } from './labels'; 31 | export { Collections } from './collections'; 32 | export { Evals } from './evals'; 33 | -------------------------------------------------------------------------------- /src/apis/labels.ts: -------------------------------------------------------------------------------- 1 | import { LABELS_API } from '../constants'; 2 | import { ApiClientInterface } from '../_types/generalTypes'; 3 | import { ApiResource } from '../apiResource'; 4 | import { APIPromise, RequestOptions } from '../baseClient'; 5 | import { overrideConfig, toQueryParams } from '../utils'; 6 | import { createHeaders } from './createHeaders'; 7 | 8 | export interface LabelBody { 9 | name: string; 10 | organisation_id?: string; 11 | workspace_id?: string; 12 | description?: string; 13 | color_code?: string; 14 | } 15 | 16 | export interface LabelsQuery { 17 | organisation_id?: string; 18 | workspace_id?: string; 19 | search?: string; 20 | current_page?: number; 21 | page_size?: number; 22 | } 23 | 24 | export class Labels extends ApiResource { 25 | create( 26 | _body: LabelBody, 27 | params?: ApiClientInterface, 28 | opts?: RequestOptions 29 | ): APIPromise { 30 | const body = _body; 31 | if (params) { 32 | const config = overrideConfig(this.client.config, params.config); 33 | this.client.customHeaders = { 34 | ...this.client.customHeaders, 35 | ...createHeaders({ ...params, config }), 36 | }; 37 | } 38 | const response = this.post(`${LABELS_API}`, { 39 | body, 40 | ...opts, 41 | }); 42 | return response; 43 | } 44 | 45 | list( 46 | _query?: LabelsQuery, 47 | params?: ApiClientInterface, 48 | opts?: RequestOptions 49 | ): APIPromise { 50 | const query = _query ? toQueryParams(_query) : ''; 51 | if (params) { 52 | const config = overrideConfig(this.client.config, params.config); 53 | this.client.customHeaders = { 54 | ...this.client.customHeaders, 55 | ...createHeaders({ ...params, config }), 56 | }; 57 | } 58 | const response = this.getMethod(`${LABELS_API}${query}`, { 59 | ...opts, 60 | }); 61 | return response; 62 | } 63 | 64 | retrieve( 65 | labelId: string, 66 | params?: ApiClientInterface, 67 | opts?: RequestOptions 68 | ): APIPromise { 69 | if (params) { 70 | const config = overrideConfig(this.client.config, params.config); 71 | this.client.customHeaders = { 72 | ...this.client.customHeaders, 73 | ...createHeaders({ ...params, config }), 74 | }; 75 | } 76 | const response = this.getMethod(`${LABELS_API}/${labelId}`, { 77 | ...opts, 78 | }); 79 | return response; 80 | } 81 | 82 | update( 83 | labelId: string, 84 | _body?: { 85 | name?: string; 86 | description?: string; 87 | color_code?: string; 88 | }, 89 | params?: ApiClientInterface, 90 | opts?: RequestOptions 91 | ): APIPromise { 92 | const body = _body; 93 | if (params) { 94 | const config = overrideConfig(this.client.config, params.config); 95 | this.client.customHeaders = { 96 | ...this.client.customHeaders, 97 | ...createHeaders({ ...params, config }), 98 | }; 99 | } 100 | const response = this.put(`${LABELS_API}/${labelId}`, { 101 | body, 102 | ...opts, 103 | }); 104 | return response; 105 | } 106 | 107 | delete( 108 | labelId: string, 109 | params?: ApiClientInterface, 110 | opts?: RequestOptions 111 | ): APIPromise { 112 | if (params) { 113 | const config = overrideConfig(this.client.config, params.config); 114 | this.client.customHeaders = { 115 | ...this.client.customHeaders, 116 | ...createHeaders({ ...params, config }), 117 | }; 118 | } 119 | const response = this.deleteMethod(`${LABELS_API}/${labelId}`, { 120 | ...opts, 121 | }); 122 | return response; 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /src/apis/logsExport.ts: -------------------------------------------------------------------------------- 1 | import { ApiResource } from '../apiResource'; 2 | import { APIResponseType, ApiClientInterface } from '../_types/generalTypes'; 3 | import { APIPromise, RequestOptions } from '../baseClient'; 4 | import { createHeaders } from './createHeaders'; 5 | import { overrideConfig, toQueryParams } from '../utils'; 6 | import { LOGS_API } from '../constants'; 7 | 8 | export interface LogsExportCreateParams { 9 | filters?: Record; 10 | workspaceId?: string; 11 | description?: string; 12 | requestedData?: string[]; 13 | } 14 | export interface LogsExportCreateResponse extends APIResponseType { 15 | id?: string; 16 | total?: number; 17 | object?: string; 18 | } 19 | export interface LogsExportListParams { 20 | workspaceId?: string; 21 | } 22 | export interface LogsExportListResponse extends APIResponseType { 23 | object?: string; 24 | total?: number; 25 | data?: Record[]; 26 | } 27 | export interface LogsExportUpdateParams { 28 | exportId?: string; 29 | workspaceId?: string; 30 | filters?: Record; 31 | requestedData?: string[]; 32 | } 33 | export interface LogsExportUpdateResponse extends APIResponseType { 34 | id?: string; 35 | total?: number; 36 | object?: string; 37 | } 38 | export interface LogsExportCancelParams { 39 | exportId?: string; 40 | } 41 | export interface LogsExportCancelResponse extends APIResponseType { 42 | message?: string; 43 | object?: string; 44 | } 45 | export interface LogsExportRetrieveParams { 46 | exportId: string; 47 | } 48 | export interface LogsExportRetrieveResponse extends APIResponseType { 49 | id?: string; 50 | organisation_id?: string; 51 | filters?: Record; 52 | requested_data?: string[]; 53 | status?: string; 54 | description?: string; 55 | created_at?: Date; 56 | last_updated_at?: Date; 57 | created_by?: string; 58 | workspace_id?: string; 59 | total_records?: number; 60 | object?: string; 61 | } 62 | export interface LogsExportStartParams { 63 | exportId?: string; 64 | } 65 | export interface LogsExportStartResponse extends APIResponseType { 66 | message?: string; 67 | object?: string; 68 | } 69 | export interface LogsExportDownloadParams { 70 | exportId?: string; 71 | } 72 | export interface LogsExportDownloadResponse extends APIResponseType { 73 | signed_url?: string; 74 | } 75 | 76 | export interface LogInsertBody { 77 | request?: Record; 78 | response?: Record; 79 | metadata?: Record; 80 | } 81 | export class Logs extends ApiResource { 82 | exports: Exports; 83 | constructor(client: any) { 84 | super(client); 85 | this.exports = new Exports(client); 86 | } 87 | 88 | create( 89 | _body: LogInsertBody, 90 | params?: ApiClientInterface, 91 | opts?: RequestOptions 92 | ): APIPromise { 93 | const body = _body; 94 | if (params) { 95 | const config = overrideConfig(this.client.config, params.config); 96 | this.client.customHeaders = { 97 | ...this.client.customHeaders, 98 | ...createHeaders({ ...params, config }), 99 | }; 100 | } 101 | const response = this.post(LOGS_API, { 102 | body, 103 | ...opts, 104 | }); 105 | return response; 106 | } 107 | } 108 | export class Exports extends ApiResource { 109 | create( 110 | _body: LogsExportCreateParams, 111 | params?: ApiClientInterface, 112 | opts?: RequestOptions 113 | ): APIPromise { 114 | const { workspaceId, requestedData, ...rest } = _body; 115 | const body = { 116 | ...rest, 117 | workspace_id: workspaceId, 118 | requested_data: requestedData, 119 | }; 120 | if (params) { 121 | this.client.customHeaders = { 122 | ...this.client.customHeaders, 123 | ...createHeaders({ ...params }), 124 | }; 125 | } 126 | const response = this.post('/logs/exports', { 127 | body, 128 | ...opts, 129 | }); 130 | return response; 131 | } 132 | retrieve( 133 | _body: LogsExportRetrieveParams, 134 | params?: ApiClientInterface, 135 | opts?: RequestOptions 136 | ): APIPromise { 137 | const body = _body; 138 | const exportId = body.exportId; 139 | if (params) { 140 | this.client.customHeaders = { 141 | ...this.client.customHeaders, 142 | ...createHeaders({ ...params }), 143 | }; 144 | } 145 | const response = this.getMethod( 146 | `/logs/exports/${exportId}`, 147 | { ...opts } 148 | ); 149 | return response; 150 | } 151 | list( 152 | _body: LogsExportListParams, 153 | params?: ApiClientInterface, 154 | opts?: RequestOptions 155 | ): APIPromise { 156 | const { workspaceId, ...rest } = _body; 157 | const body = { 158 | ...rest, 159 | workspace_id: workspaceId, 160 | }; 161 | const query = toQueryParams(body); 162 | if (params) { 163 | this.client.customHeaders = { 164 | ...this.client.customHeaders, 165 | ...createHeaders({ ...params }), 166 | }; 167 | } 168 | const response = this.getMethod( 169 | `/logs/exports${query}`, 170 | { ...opts } 171 | ); 172 | return response; 173 | } 174 | update( 175 | _body: LogsExportUpdateParams, 176 | params?: ApiClientInterface, 177 | opts?: RequestOptions 178 | ): APIPromise { 179 | const { workspaceId, requestedData, ...rest } = _body; 180 | const body = { 181 | ...rest, 182 | workspace_id: workspaceId, 183 | requested_data: requestedData, 184 | }; 185 | const exportId = body.exportId; 186 | if (params) { 187 | this.client.customHeaders = { 188 | ...this.client.customHeaders, 189 | ...createHeaders({ ...params }), 190 | }; 191 | } 192 | const response = this.put( 193 | `/logs/exports/${exportId}`, 194 | { body, ...opts } 195 | ); 196 | return response; 197 | } 198 | start( 199 | _body: LogsExportStartParams, 200 | params?: ApiClientInterface, 201 | opts?: RequestOptions 202 | ): APIPromise { 203 | const body = _body; 204 | const exportId = body.exportId; 205 | if (params) { 206 | this.client.customHeaders = { 207 | ...this.client.customHeaders, 208 | ...createHeaders({ ...params }), 209 | }; 210 | } 211 | const response = this.post( 212 | `/logs/exports/${exportId}/start`, 213 | { body, ...opts } 214 | ); 215 | return response; 216 | } 217 | cancel( 218 | _body: LogsExportCancelParams, 219 | params?: ApiClientInterface, 220 | opts?: RequestOptions 221 | ): APIPromise { 222 | const body = _body; 223 | const exportId = body.exportId; 224 | if (params) { 225 | this.client.customHeaders = { 226 | ...this.client.customHeaders, 227 | ...createHeaders({ ...params }), 228 | }; 229 | } 230 | const response = this.post( 231 | `/logs/exports/${exportId}/cancel`, 232 | { body, ...opts } 233 | ); 234 | return response; 235 | } 236 | download( 237 | _body: LogsExportDownloadParams, 238 | params?: ApiClientInterface, 239 | opts?: RequestOptions 240 | ): APIPromise { 241 | const body = _body; 242 | const exportId = body.exportId; 243 | if (params) { 244 | this.client.customHeaders = { 245 | ...this.client.customHeaders, 246 | ...createHeaders({ ...params }), 247 | }; 248 | } 249 | const response = this.getMethod( 250 | `/logs/exports/${exportId}/download`, 251 | { body, ...opts } 252 | ); 253 | return response; 254 | } 255 | } 256 | -------------------------------------------------------------------------------- /src/apis/models.ts: -------------------------------------------------------------------------------- 1 | import { ApiClientInterface } from '../_types/generalTypes'; 2 | import { ApiResource } from '../apiResource'; 3 | import { RequestOptions } from '../baseClient'; 4 | import { finalResponse, initOpenAIClient, overrideConfig } from '../utils'; 5 | import { createHeaders } from './createHeaders'; 6 | 7 | export class Models extends ApiResource { 8 | async list(params?: ApiClientInterface, opts?: RequestOptions): Promise { 9 | if (params) { 10 | const config = overrideConfig(this.client.config, params.config); 11 | this.client.customHeaders = { 12 | ...this.client.customHeaders, 13 | ...createHeaders({ ...params, config }), 14 | }; 15 | } 16 | 17 | const OAIclient = initOpenAIClient(this.client); 18 | 19 | const result = await OAIclient.models.list(opts).withResponse(); 20 | 21 | return finalResponse(result); 22 | } 23 | 24 | async retrieve( 25 | model: string, 26 | params?: ApiClientInterface, 27 | opts?: RequestOptions 28 | ): Promise { 29 | if (params) { 30 | const config = overrideConfig(this.client.config, params.config); 31 | this.client.customHeaders = { 32 | ...this.client.customHeaders, 33 | ...createHeaders({ ...params, config }), 34 | }; 35 | } 36 | 37 | const OAIclient = initOpenAIClient(this.client); 38 | 39 | const result = await OAIclient.models.retrieve(model, opts).withResponse(); 40 | 41 | return finalResponse(result); 42 | } 43 | 44 | async del( 45 | model: string, 46 | params?: ApiClientInterface, 47 | opts?: RequestOptions 48 | ): Promise { 49 | if (params) { 50 | const config = overrideConfig(this.client.config, params.config); 51 | this.client.customHeaders = { 52 | ...this.client.customHeaders, 53 | ...createHeaders({ ...params, config }), 54 | }; 55 | } 56 | 57 | const OAIclient = initOpenAIClient(this.client); 58 | 59 | const result = await OAIclient.models.del(model, opts).withResponse(); 60 | 61 | return finalResponse(result); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/apis/moderations.ts: -------------------------------------------------------------------------------- 1 | import { ApiClientInterface } from '../_types/generalTypes'; 2 | import { ApiResource } from '../apiResource'; 3 | import { RequestOptions } from '../baseClient'; 4 | import { finalResponse, initOpenAIClient, overrideConfig } from '../utils'; 5 | import { createHeaders } from './createHeaders'; 6 | 7 | export interface ModerationCreateParams { 8 | input: string | Array | Array; 9 | model?: any; 10 | [key: string]: any; 11 | } 12 | 13 | interface ModerationMultiModalInput { 14 | type: string; 15 | text?: string; 16 | image_url?: object; 17 | } 18 | 19 | export class Moderations extends ApiResource { 20 | async create( 21 | _body: ModerationCreateParams, 22 | params?: ApiClientInterface, 23 | opts?: RequestOptions 24 | ): Promise { 25 | const body: ModerationCreateParams = _body; 26 | if (params) { 27 | const config = overrideConfig(this.client.config, params.config); 28 | this.client.customHeaders = { 29 | ...this.client.customHeaders, 30 | ...createHeaders({ ...params, config }), 31 | }; 32 | } 33 | 34 | const OAIclient = initOpenAIClient(this.client); 35 | 36 | const result = await OAIclient.moderations 37 | .create(body as any, opts) 38 | .withResponse(); 39 | return finalResponse(result); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/apis/postMethod.ts: -------------------------------------------------------------------------------- 1 | import { APIResponseType, ApiClientInterface } from '../_types/generalTypes'; 2 | import { ApiResource } from '../apiResource'; 3 | import { APIPromise, RequestOptions } from '../baseClient'; 4 | import { Stream } from '../streaming'; 5 | import { overrideConfig } from '../utils'; 6 | import { createHeaders } from './createHeaders'; 7 | 8 | export class postMethod extends ApiResource { 9 | create( 10 | url: string, 11 | _body: PostBodyNonStreaming, 12 | params?: ApiClientInterface, 13 | opts?: RequestOptions 14 | ): APIPromise; 15 | create( 16 | url: string, 17 | _body: PostBodyStreaming, 18 | params?: ApiClientInterface, 19 | opts?: RequestOptions 20 | ): APIPromise>; 21 | create( 22 | url: string, 23 | _body: PostBodyParams, 24 | params?: ApiClientInterface, 25 | opts?: RequestOptions 26 | ): APIPromise> | APIPromise; 27 | create( 28 | url: string, 29 | _body: PostBodyParams, 30 | params?: ApiClientInterface, 31 | opts?: RequestOptions 32 | ): APIPromise | APIPromise> { 33 | const body = _body; 34 | if (params) { 35 | const config = overrideConfig(this.client.config, params.config); 36 | this.client.customHeaders = { 37 | ...this.client.customHeaders, 38 | ...createHeaders({ ...params, config }), 39 | }; 40 | } 41 | const response = this.post(url, { body, ...opts }) as 42 | | APIPromise 43 | | APIPromise>; 44 | return response; 45 | } 46 | } 47 | 48 | export type PostResponse = Record & APIResponseType; 49 | 50 | export interface PostBodyStreaming extends Record { 51 | stream?: true; 52 | } 53 | 54 | export interface PostBodyNonStreaming extends Record { 55 | stream?: false; 56 | } 57 | 58 | export type PostBodyParams = PostBodyNonStreaming | PostBodyStreaming; 59 | -------------------------------------------------------------------------------- /src/apis/putMethod.ts: -------------------------------------------------------------------------------- 1 | import { APIResponseType, ApiClientInterface } from '../_types/generalTypes'; 2 | import { ApiResource } from '../apiResource'; 3 | import { APIPromise, RequestOptions } from '../baseClient'; 4 | import { overrideConfig } from '../utils'; 5 | import { createHeaders } from './createHeaders'; 6 | 7 | export class putMethod extends ApiResource { 8 | create( 9 | path: string, 10 | _body: PutBodyParams, 11 | params?: ApiClientInterface, 12 | opts?: RequestOptions 13 | ): APIPromise { 14 | const body = _body; 15 | if (params) { 16 | const config = overrideConfig(this.client.config, params.config); 17 | this.client.customHeaders = { 18 | ...this.client.customHeaders, 19 | ...createHeaders({ ...params, config }), 20 | }; 21 | } 22 | const response = this.put(path, { body, ...opts }); 23 | return response; 24 | } 25 | } 26 | 27 | export type PutResponse = Record & APIResponseType; 28 | 29 | export type PutBodyParams = Record; 30 | -------------------------------------------------------------------------------- /src/apis/realtime.ts: -------------------------------------------------------------------------------- 1 | import { ApiClientInterface } from '../_types/generalTypes'; 2 | import { ApiResource } from '../apiResource'; 3 | import { SessionCreateParams } from 'openai/resources/beta/realtime/sessions'; 4 | import { finalResponse, initOpenAIClient, overrideConfig } from '../utils'; 5 | import { createHeaders } from './createHeaders'; 6 | import { RequestOptions } from '../baseClient'; 7 | 8 | export class Realtime extends ApiResource { 9 | sessions: Sessions; 10 | constructor(client: any) { 11 | super(client); 12 | this.sessions = new Sessions(client); 13 | } 14 | } 15 | 16 | export class Sessions extends ApiResource { 17 | async create( 18 | _body: SessionCreateBody, 19 | params?: ApiClientInterface, 20 | opts?: RequestOptions 21 | ): Promise { 22 | const body: SessionCreateBody = _body; 23 | if (params) { 24 | const config = overrideConfig(this.client.config, params.config); 25 | this.client.customHeaders = { 26 | ...this.client.customHeaders, 27 | ...createHeaders({ ...params, config }), 28 | }; 29 | } 30 | 31 | const OAIclient = initOpenAIClient(this.client); 32 | const result = await OAIclient.beta.realtime.sessions 33 | .create(body, opts) 34 | .withResponse(); 35 | 36 | return finalResponse(result); 37 | } 38 | } 39 | 40 | export interface SessionCreateBody extends SessionCreateParams { 41 | [key: string]: any; 42 | } 43 | -------------------------------------------------------------------------------- /src/apis/responses.ts: -------------------------------------------------------------------------------- 1 | import { ApiResource } from '../apiResource'; 2 | import { createHeaders } from './createHeaders'; 3 | import { finalResponse, initOpenAIClient, overrideConfig } from '../utils'; 4 | import { ApiClientInterface } from '../_types/generalTypes'; 5 | import { APIPromise, RequestOptions } from '../baseClient'; 6 | import { 7 | Response, 8 | ResponseCreateParams, 9 | ResponseCreateParamsBase, 10 | ResponseCreateParamsNonStreaming, 11 | ResponseCreateParamsStreaming, 12 | ResponseStreamEvent, 13 | } from 'openai/resources/responses/responses'; 14 | import { CursorPageParams } from '../_types/sharedTypes'; 15 | import { ResponseCreateParamsWithTools } from 'openai/lib/ResponsesParser'; 16 | import { ResponseStreamParams } from 'openai/lib/responses/ResponseStream'; 17 | import { Stream } from '../streaming'; 18 | 19 | export class Responses extends ApiResource { 20 | inputItems: InputItems; 21 | 22 | constructor(client: any) { 23 | super(client); 24 | this.inputItems = new InputItems(client); 25 | } 26 | 27 | create( 28 | body: ResponseCreateParamsNonStreaming, 29 | params?: ApiClientInterface, 30 | opts?: RequestOptions 31 | ): APIPromise; 32 | create( 33 | body: ResponseCreateParamsStreaming, 34 | params?: ApiClientInterface, 35 | opts?: RequestOptions 36 | ): APIPromise>; 37 | create( 38 | body: ResponseCreateParamsBase, 39 | params?: ApiClientInterface, 40 | opts?: RequestOptions 41 | ): APIPromise | Response>; 42 | create( 43 | body: ResponseCreateParams, 44 | params?: ApiClientInterface, 45 | opts?: RequestOptions 46 | ): APIPromise | APIPromise> { 47 | if (params) { 48 | const config = overrideConfig(this.client.config, params.config); 49 | this.client.customHeaders = { 50 | ...this.client.customHeaders, 51 | ...createHeaders({ ...params, config }), 52 | }; 53 | } 54 | 55 | const OAIclient = initOpenAIClient(this.client); 56 | 57 | const result = OAIclient.responses.create(body, opts); 58 | 59 | return result as any; 60 | } 61 | 62 | async retrieve( 63 | responseId: string, 64 | _query?: ResponseRetrieveParams, 65 | params?: ApiClientInterface, 66 | opts?: RequestOptions 67 | ): Promise { 68 | const query: ResponseRetrieveParams | undefined = _query; 69 | if (params) { 70 | const config = overrideConfig(this.client.config, params.config); 71 | this.client.customHeaders = { 72 | ...this.client.customHeaders, 73 | ...createHeaders({ ...params, config }), 74 | }; 75 | } 76 | 77 | const OAIclient = initOpenAIClient(this.client); 78 | 79 | const result = await OAIclient.responses 80 | .retrieve(responseId, query, opts) 81 | .withResponse(); 82 | 83 | return finalResponse(result); 84 | } 85 | 86 | async del( 87 | responseId: string, 88 | params?: ApiClientInterface, 89 | opts?: RequestOptions 90 | ): Promise { 91 | if (params) { 92 | const config = overrideConfig(this.client.config, params.config); 93 | this.client.customHeaders = { 94 | ...this.client.customHeaders, 95 | ...createHeaders({ ...params, config }), 96 | }; 97 | } 98 | 99 | const OAIclient = initOpenAIClient(this.client); 100 | 101 | const result = await OAIclient.responses 102 | .del(responseId, opts) 103 | .withResponse(); 104 | 105 | return finalResponse(result); 106 | } 107 | 108 | async parse( 109 | _body: Params, 110 | params?: ApiClientInterface, 111 | opts?: RequestOptions 112 | ): Promise { 113 | const body: Params = _body; 114 | if (params) { 115 | const config = overrideConfig(this.client.config, params.config); 116 | this.client.customHeaders = { 117 | ...this.client.customHeaders, 118 | ...createHeaders({ ...params, config }), 119 | }; 120 | } 121 | 122 | const OAIclient = initOpenAIClient(this.client); 123 | 124 | const result = await OAIclient.responses.parse(body, opts); 125 | 126 | return result; 127 | } 128 | 129 | async stream( 130 | _body: Params, 131 | params?: ApiClientInterface, 132 | opts?: RequestOptions 133 | ): Promise { 134 | const body: Params = _body; 135 | if (params) { 136 | const config = overrideConfig(this.client.config, params.config); 137 | this.client.customHeaders = { 138 | ...this.client.customHeaders, 139 | ...createHeaders({ ...params, config }), 140 | }; 141 | } 142 | 143 | const OAIclient = initOpenAIClient(this.client); 144 | 145 | const result = await OAIclient.responses.stream(body, opts); 146 | 147 | return result; 148 | } 149 | } 150 | 151 | export class InputItems extends ApiResource { 152 | async list( 153 | responseId: string, 154 | _query?: InputItemListParams, 155 | params?: ApiClientInterface, 156 | opts?: RequestOptions 157 | ): Promise { 158 | const query: InputItemListParams | undefined = _query; 159 | if (params) { 160 | const config = overrideConfig(this.client.config, params.config); 161 | this.client.customHeaders = { 162 | ...this.client.customHeaders, 163 | ...createHeaders({ ...params, config }), 164 | }; 165 | } 166 | 167 | const OAIclient = initOpenAIClient(this.client); 168 | 169 | const result = await OAIclient.responses.inputItems 170 | .list(responseId, query, opts) 171 | .withResponse(); 172 | 173 | return finalResponse(result); 174 | } 175 | } 176 | 177 | export interface ResponseRetrieveParams { 178 | include?: Array; 179 | } 180 | 181 | export interface InputItemListParams extends CursorPageParams { 182 | before?: string; 183 | include?: Array; 184 | order?: 'asc' | 'desc'; 185 | [key: string]: any; 186 | } 187 | 188 | export type ResponseIncludable = 189 | | 'file_search_call.results' 190 | | 'message.input_image.image_url' 191 | | 'computer_call_output.output.image_url'; 192 | -------------------------------------------------------------------------------- /src/apis/uploads.ts: -------------------------------------------------------------------------------- 1 | import { ApiClientInterface } from '../_types/generalTypes'; 2 | import { ApiResource } from '../apiResource'; 3 | import { RequestOptions } from '../baseClient'; 4 | import { finalResponse, initOpenAIClient, overrideConfig } from '../utils'; 5 | import { createHeaders } from './createHeaders'; 6 | import { Uploadable } from 'openai/uploads'; 7 | 8 | export interface UploadCompleteParams { 9 | part_ids: Array; 10 | md5?: string; 11 | [key: string]: any; 12 | } 13 | 14 | export class Uploads extends ApiResource { 15 | parts: Parts; 16 | 17 | constructor(client: any) { 18 | super(client); 19 | this.parts = new Parts(client); 20 | } 21 | 22 | async create( 23 | _body: UploadCreateParams, 24 | params?: ApiClientInterface, 25 | opts?: RequestOptions 26 | ): Promise { 27 | const body: UploadCreateParams = _body; 28 | if (params) { 29 | const config = overrideConfig(this.client.config, params.config); 30 | this.client.customHeaders = { 31 | ...this.client.customHeaders, 32 | ...createHeaders({ ...params, config }), 33 | }; 34 | } 35 | const OAIclient = initOpenAIClient(this.client); 36 | const response = await OAIclient.uploads.create(body, opts).withResponse(); 37 | return finalResponse(response); 38 | } 39 | 40 | async cancel( 41 | uploadId: string, 42 | params?: ApiClientInterface, 43 | opts?: RequestOptions 44 | ): Promise { 45 | if (params) { 46 | const config = overrideConfig(this.client.config, params.config); 47 | this.client.customHeaders = { 48 | ...this.client.customHeaders, 49 | ...createHeaders({ ...params, config }), 50 | }; 51 | } 52 | const OAIclient = initOpenAIClient(this.client); 53 | const body = {}; 54 | const options = { body, ...opts }; 55 | const response = await OAIclient.uploads 56 | .cancel(uploadId, options) 57 | .withResponse(); 58 | return finalResponse(response); 59 | } 60 | 61 | async complete( 62 | uploadId: string, 63 | _body: UploadCompleteParams, 64 | params?: ApiClientInterface, 65 | opts?: RequestOptions 66 | ): Promise { 67 | const body: UploadCompleteParams = _body; 68 | if (params) { 69 | const config = overrideConfig(this.client.config, params.config); 70 | this.client.customHeaders = { 71 | ...this.client.customHeaders, 72 | ...createHeaders({ ...params, config }), 73 | }; 74 | } 75 | const OAIclient = initOpenAIClient(this.client); 76 | const response = await OAIclient.uploads 77 | .complete(uploadId, body, opts) 78 | .withResponse(); 79 | return finalResponse(response); 80 | } 81 | } 82 | 83 | export class Parts extends ApiResource { 84 | async create( 85 | uploadId: string, 86 | _body: PartCreateParams, 87 | params?: ApiClientInterface, 88 | opts?: RequestOptions 89 | ): Promise { 90 | const body: PartCreateParams = _body; 91 | if (params) { 92 | const config = overrideConfig(this.client.config, params.config); 93 | this.client.customHeaders = { 94 | ...this.client.customHeaders, 95 | ...createHeaders({ ...params, config }), 96 | }; 97 | } 98 | const OAIclient = initOpenAIClient(this.client); 99 | const response = await OAIclient.uploads.parts 100 | .create(uploadId, body, opts) 101 | .withResponse(); 102 | return finalResponse(response); 103 | } 104 | } 105 | 106 | export interface UploadCreateParams { 107 | bytes: number; 108 | filename: string; 109 | mime_type: string; 110 | purpose: 'assistants' | 'batch' | 'fine-tune' | 'vision'; 111 | [key: string]: any; 112 | } 113 | 114 | export interface PartCreateParams { 115 | data: Uploadable; 116 | [key: string]: any; 117 | } 118 | -------------------------------------------------------------------------------- /src/apis/virtualKeys.ts: -------------------------------------------------------------------------------- 1 | import { ApiResource } from '../apiResource'; 2 | import { APIResponseType, ApiClientInterface } from '../_types/generalTypes'; 3 | import { APIPromise, RequestOptions } from '../baseClient'; 4 | import { createHeaders } from './createHeaders'; 5 | import { toQueryParams } from '../utils'; 6 | 7 | export interface VirtualKeysAddParams { 8 | name?: string; 9 | provider?: string; 10 | key?: string; 11 | note?: string | null; 12 | apiVersion?: string | null; 13 | resourceName?: string | null; 14 | deploymentName?: string | null; 15 | workspace_id?: string; 16 | usage_limits?: Record; 17 | [key: string]: any; 18 | } 19 | export interface VirtualKeysAddResponse extends APIResponseType { 20 | id?: string; 21 | slug?: string; 22 | object?: string; 23 | } 24 | export interface VirtualKeysGetParams { 25 | slug?: string; 26 | } 27 | 28 | export interface VirtualKeysGetResponse extends APIResponseType { 29 | id?: string; 30 | ai_provider_name?: string; 31 | model_config?: Record; 32 | masked_api_key?: string; 33 | slug?: string; 34 | name?: string; 35 | usage_limits?: Record; 36 | status?: string; 37 | note?: null | string; 38 | created_at?: Date; 39 | rate_limits?: Record[]; 40 | object?: string; 41 | } 42 | export interface VirtualKeysListParams { 43 | workspace_id?: string; 44 | } 45 | export interface VirtualKeysListResponse extends APIResponseType { 46 | object?: string; 47 | total?: number; 48 | data?: VirtualKeysGetResponse[]; 49 | } 50 | 51 | export interface VirtualKeysUpdateParams { 52 | slug?: string; 53 | name?: string; 54 | key?: string; 55 | note?: string | null; 56 | usage_limits?: Record; 57 | rate_limits?: Record[]; 58 | [key: string]: any; 59 | } 60 | export interface VirtualKeysUpdateResponse extends APIResponseType { 61 | id?: string; 62 | slug?: string; 63 | object?: string; 64 | } 65 | export interface VirtualKeysDeleteParams { 66 | slug?: string; 67 | } 68 | 69 | export class VirtualKeys extends ApiResource { 70 | create( 71 | body: VirtualKeysAddParams, 72 | params?: ApiClientInterface, 73 | opts?: RequestOptions 74 | ): APIPromise { 75 | if (params) { 76 | this.client.customHeaders = { 77 | ...this.client.customHeaders, 78 | ...createHeaders({ ...params }), 79 | }; 80 | } 81 | const response = this.post('/virtual-keys', { 82 | body, 83 | ...opts, 84 | }); 85 | return response; 86 | } 87 | 88 | list( 89 | _body?: VirtualKeysListParams, 90 | params?: ApiClientInterface, 91 | opts?: RequestOptions 92 | ): APIPromise { 93 | const body = _body; 94 | if (params) { 95 | this.client.customHeaders = { 96 | ...this.client.customHeaders, 97 | ...createHeaders({ ...params }), 98 | }; 99 | } 100 | const query = toQueryParams(body); 101 | const response = this.getMethod( 102 | `/virtual-keys${query}`, 103 | { ...opts } 104 | ); 105 | return response; 106 | } 107 | 108 | retrieve( 109 | body: VirtualKeysGetParams, 110 | params?: ApiClientInterface, 111 | opts?: RequestOptions 112 | ): APIPromise { 113 | const { slug } = body; 114 | if (params) { 115 | this.client.customHeaders = { 116 | ...this.client.customHeaders, 117 | ...createHeaders({ ...params }), 118 | }; 119 | } 120 | const response = this.getMethod( 121 | `/virtual-keys/${slug}`, 122 | { ...opts } 123 | ); 124 | return response; 125 | } 126 | 127 | update( 128 | body: VirtualKeysUpdateParams, 129 | params?: ApiClientInterface, 130 | opts?: RequestOptions 131 | ): APIPromise { 132 | const { slug, ...restBody } = body; 133 | if (params) { 134 | this.client.customHeaders = { 135 | ...this.client.customHeaders, 136 | ...createHeaders({ ...params }), 137 | }; 138 | } 139 | const response = this.put( 140 | `/virtual-keys/${slug}`, 141 | { body: restBody, ...opts } 142 | ); 143 | return response; 144 | } 145 | 146 | delete( 147 | body: VirtualKeysDeleteParams, 148 | params?: ApiClientInterface, 149 | opts?: RequestOptions 150 | ): APIPromise { 151 | const { slug } = body; 152 | if (params) { 153 | this.client.customHeaders = { 154 | ...this.client.customHeaders, 155 | ...createHeaders({ ...params }), 156 | }; 157 | } 158 | const response = this.deleteMethod(`/virtual-keys/${slug}`, { 159 | ...opts, 160 | }); 161 | return response; 162 | } 163 | } 164 | -------------------------------------------------------------------------------- /src/beta/realtime/index.ts: -------------------------------------------------------------------------------- 1 | export { PortkeyAIRealtimeWS } from './ws'; 2 | -------------------------------------------------------------------------------- /src/beta/realtime/ws.ts: -------------------------------------------------------------------------------- 1 | import { OpenAIRealtimeWS } from 'openai/beta/realtime/ws'; 2 | import WebSocket from 'ws'; 3 | 4 | export class PortkeyAIRealtimeWS extends OpenAIRealtimeWS { 5 | override url!: URL; 6 | override socket!: WebSocket; 7 | 8 | constructor(props: { model: string; options?: any }, client: any) { 9 | const options = { 10 | ...props.options, 11 | headers: { 12 | ...(props.options?.headers || {}), 13 | ...client.customHeaders, 14 | }, 15 | }; 16 | 17 | super({ ...props, options }, client); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/client.ts: -------------------------------------------------------------------------------- 1 | import { ApiClientInterface } from './_types/generalTypes'; 2 | import * as API from './apis'; 3 | import { DeleteResponse } from './apis/deleteMethod'; 4 | import { GetResponse } from './apis/getMethod'; 5 | import { PostBodyParams, PostResponse } from './apis/postMethod'; 6 | import { PutBodyParams, PutResponse } from './apis/putMethod'; 7 | import { ApiClient, APIPromise, RequestOptions } from './baseClient'; 8 | import { isRunningInBrowser } from './core'; 9 | import { Stream } from './streaming'; 10 | import { readEnv, setApiKey, setBaseURL } from './utils'; 11 | 12 | export class Portkey extends ApiClient { 13 | declare apiKey: string | null; 14 | declare baseURL: string; 15 | virtualKey: string | null; 16 | config: Record | string | null | undefined; 17 | provider: string | null | undefined; 18 | traceID: string | null | undefined; 19 | metadata: Record | null | undefined; 20 | Authorization?: string; 21 | cacheForceRefresh?: boolean | null | undefined; 22 | debug?: boolean | null | undefined; 23 | customHost?: string | null | undefined; 24 | openaiProject?: string | null | undefined; 25 | openaiOrganization?: string | null | undefined; 26 | awsSecretAccessKey?: string | null | undefined; 27 | awsAccessKeyId?: string | null | undefined; 28 | awsSessionToken?: string | null | undefined; 29 | awsRegion?: string | null | undefined; 30 | vertexProjectId?: string | null | undefined; 31 | vertexRegion?: string | null | undefined; 32 | workersAiAccountId?: string | null | undefined; 33 | azureResourceName?: string | null | undefined; 34 | azureDeploymentId?: string | null | undefined; 35 | azureApiVersion?: string | null | undefined; 36 | azureEndpointName?: string | null | undefined; 37 | huggingfaceBaseUrl?: string | null | undefined; 38 | forwardHeaders?: Array | null | undefined; 39 | requestTimeout?: number | null | undefined; 40 | cacheNamespace?: string | null | undefined; 41 | strictOpenAiCompliance?: boolean | null | undefined; 42 | anthropicBeta?: string | null | undefined; 43 | anthropicVersion?: string | null | undefined; 44 | mistralFimCompletion?: string | null | undefined; 45 | dangerouslyAllowBrowser?: boolean | null | undefined; 46 | vertexStorageBucketName?: string | null | undefined; 47 | providerFileName?: string | null | undefined; 48 | providerModel?: string | null | undefined; 49 | awsS3Bucket?: string | null | undefined; 50 | awsS3ObjectKey?: string | null | undefined; 51 | awsBedrockModel?: string | null | undefined; 52 | fireworksAccountId?: string | null | undefined; 53 | calculateAudioDuration: boolean | null | undefined; 54 | constructor({ 55 | apiKey = readEnv('PORTKEY_API_KEY') ?? null, 56 | baseURL = readEnv('PORTKEY_BASE_URL') ?? null, 57 | config, 58 | virtualKey, 59 | provider, 60 | traceID, 61 | metadata, 62 | Authorization, 63 | cacheForceRefresh, 64 | debug, 65 | customHost, 66 | openaiProject, 67 | openaiOrganization, 68 | awsSecretAccessKey, 69 | awsAccessKeyId, 70 | awsSessionToken, 71 | awsRegion, 72 | vertexProjectId, 73 | vertexRegion, 74 | workersAiAccountId, 75 | azureResourceName, 76 | azureDeploymentId, 77 | azureApiVersion, 78 | azureEndpointName, 79 | huggingfaceBaseUrl, 80 | forwardHeaders, 81 | cacheNamespace, 82 | requestTimeout, 83 | strictOpenAiCompliance, 84 | anthropicBeta, 85 | anthropicVersion, 86 | mistralFimCompletion, 87 | dangerouslyAllowBrowser, 88 | vertexStorageBucketName, 89 | providerFileName, 90 | providerModel, 91 | awsS3Bucket, 92 | awsS3ObjectKey, 93 | awsBedrockModel, 94 | fireworksAccountId, 95 | calculateAudioDuration, 96 | ...rest 97 | }: ApiClientInterface) { 98 | if (isRunningInBrowser() && !dangerouslyAllowBrowser) { 99 | throw new Error( 100 | "It looks like you're running in a browser-like environment.\n\nThis is disabled by default, as it risks exposing your secret API credentials to attackers.\nIf you understand the risks and have appropriate mitigations in place,\nyou can set the `dangerouslyAllowBrowser` option to `true`, e.g.,\n\nnew Portkey({ ..., dangerouslyAllowBrowser: true, ... });" 101 | ); 102 | } 103 | super({ 104 | apiKey, 105 | baseURL, 106 | config, 107 | virtualKey, 108 | provider, 109 | traceID, 110 | metadata, 111 | Authorization, 112 | cacheForceRefresh, 113 | debug, 114 | customHost, 115 | cacheNamespace, 116 | openaiProject, 117 | openaiOrganization, 118 | awsSecretAccessKey, 119 | awsAccessKeyId, 120 | awsSessionToken, 121 | awsRegion, 122 | vertexProjectId, 123 | vertexRegion, 124 | workersAiAccountId, 125 | azureResourceName, 126 | azureDeploymentId, 127 | azureApiVersion, 128 | azureEndpointName, 129 | huggingfaceBaseUrl, 130 | forwardHeaders, 131 | requestTimeout, 132 | strictOpenAiCompliance, 133 | anthropicBeta, 134 | anthropicVersion, 135 | mistralFimCompletion, 136 | dangerouslyAllowBrowser, 137 | vertexStorageBucketName, 138 | providerFileName, 139 | providerModel, 140 | awsS3Bucket, 141 | awsS3ObjectKey, 142 | awsBedrockModel, 143 | fireworksAccountId, 144 | ...rest, 145 | }); 146 | this.baseURL = setBaseURL(baseURL, apiKey); 147 | this.apiKey = setApiKey(this.baseURL, apiKey); 148 | this.virtualKey = virtualKey || null; 149 | this.config = config || null; 150 | this.provider = provider; 151 | this.traceID = traceID; 152 | this.metadata = metadata; 153 | this.cacheForceRefresh = cacheForceRefresh; 154 | this.debug = debug; 155 | this.customHost = customHost; 156 | this.cacheNamespace = cacheNamespace; 157 | this.openaiProject = openaiProject; 158 | this.openaiOrganization = openaiOrganization; 159 | this.awsSecretAccessKey = awsSecretAccessKey; 160 | this.awsAccessKeyId = awsAccessKeyId; 161 | this.awsSessionToken = awsSessionToken; 162 | this.awsRegion = awsRegion; 163 | this.vertexProjectId = vertexProjectId; 164 | this.vertexRegion = vertexRegion; 165 | this.workersAiAccountId = workersAiAccountId; 166 | this.azureResourceName = azureResourceName; 167 | this.azureDeploymentId = azureDeploymentId; 168 | this.azureApiVersion = azureApiVersion; 169 | this.azureEndpointName = azureEndpointName; 170 | this.huggingfaceBaseUrl = huggingfaceBaseUrl; 171 | this.forwardHeaders = forwardHeaders; 172 | this.requestTimeout = requestTimeout; 173 | this.strictOpenAiCompliance = strictOpenAiCompliance; 174 | this.anthropicBeta = anthropicBeta; 175 | this.anthropicVersion = anthropicVersion; 176 | this.mistralFimCompletion = mistralFimCompletion; 177 | this.dangerouslyAllowBrowser = dangerouslyAllowBrowser ?? false; 178 | this.vertexStorageBucketName = vertexStorageBucketName; 179 | this.providerFileName = providerFileName; 180 | this.providerModel = providerModel; 181 | this.awsS3Bucket = awsS3Bucket; 182 | this.awsS3ObjectKey = awsS3ObjectKey; 183 | this.awsBedrockModel = awsBedrockModel; 184 | this.fireworksAccountId = fireworksAccountId; 185 | this.calculateAudioDuration = calculateAudioDuration ?? true; 186 | } 187 | 188 | completions: API.Completions = new API.Completions(this); 189 | chat = new API.Chat(this); 190 | embeddings = new API.Embeddings(this); 191 | files = new API.MainFiles(this); 192 | images = new API.Images(this); 193 | models = new API.Models(this); 194 | generations = new API.Generations(this); 195 | prompts = new API.Prompt(this); 196 | labels = new API.Labels(this); 197 | collections = new API.Collections(this); 198 | feedback = new API.Feedback(this); 199 | batches = new API.Batches(this); 200 | fineTuning = new API.FineTuning(this); 201 | vectorStores = new API.VectorStores(this); 202 | moderations = new API.Moderations(this); 203 | audio = new API.Audio(this); 204 | uploads = new API.Uploads(this); 205 | responses = new API.Responses(this); 206 | evals = new API.Evals(this); 207 | admin = new API.Admin(this); 208 | virtualKeys = new API.VirtualKeys(this); 209 | apiKeys = new API.ApiKeys(this); 210 | configs = new API.Configs(this); 211 | logs = new API.Logs(this); 212 | beta = { 213 | assistants: new API.Assistants(this), 214 | threads: new API.Threads(this), 215 | chat: new API.BetaChat(this), 216 | realtime: new API.Realtime(this), 217 | }; 218 | 219 | post = ( 220 | url: string, 221 | _body: PostBodyParams, 222 | params?: ApiClientInterface, 223 | opts?: RequestOptions 224 | ): APIPromise> | APIPromise => { 225 | return new API.postMethod(this).create(url, _body, params, opts); 226 | }; 227 | 228 | get = ( 229 | path: string, 230 | params?: ApiClientInterface, 231 | opts?: RequestOptions 232 | ): APIPromise => { 233 | return new API.getMethod(this).create(path, params, opts); 234 | }; 235 | 236 | delete = ( 237 | path: string, 238 | params?: ApiClientInterface, 239 | opts?: RequestOptions 240 | ): APIPromise => { 241 | return new API.deleteMethod(this).create(path, params, opts); 242 | }; 243 | 244 | put = ( 245 | url: string, 246 | _body: PutBodyParams, 247 | params?: ApiClientInterface, 248 | opts?: RequestOptions 249 | ): APIPromise => { 250 | return new API.putMethod(this).create(url, _body, params, opts); 251 | }; 252 | } 253 | -------------------------------------------------------------------------------- /src/constants.ts: -------------------------------------------------------------------------------- 1 | export const MISSING_API_KEY_ERROR_MESSAGE = `Portkey API Key Not Found 2 | Resolution: \ 3 | 4 | 1. Get your Portkey API key from https://app.portkey.ai/api-keys \ 5 | 6 | 2. Pass it while instantiating the Portkey client with apiKey param, or set it as an environment variable with export PORTKEY_API_KEY=YOUR_API_KEY 7 | `; 8 | 9 | export const MISSING_BASE_URL = `No Base url provided. Please provide a valid base url. 10 | For example: https://api.portkey.ai 11 | `; 12 | 13 | export const MISSING_CONFIG_MESSAGE = 14 | "The 'config' parameter is not set. Please provide a valid Config object"; 15 | 16 | export const MISSING_MODE_MESSAGE = 17 | "The 'mode' parameter is not set. Please provide a valid mode literal."; 18 | 19 | export const INVALID_PORTKEY_MODE = `Argument of type '{}' cannot be assigned to parameter "mode" of \ 20 | type "ModesLiteral | Modes | None" 21 | `; 22 | 23 | export const LOCALHOST_CONNECTION_ERROR = `Could not instantiate the Portkey client. \ 24 | You can either add a valid 'apiKey' parameter (from https://app.portkey.ai/api-keys) \ 25 | or set the 'baseURL' parameter to your AI Gateway's instance's URL.`; 26 | 27 | export const CUSTOM_HOST_CONNECTION_ERROR = `We could not connect to the AI Gateway's instance. \ 28 | Please check the 'baseURL' parameter in the Portkey client.`; 29 | 30 | export const DEFAULT_MAX_RETRIES = 2; 31 | export const DEFAULT_TIMEOUT = 60; 32 | export const PORTKEY_HEADER_PREFIX = 'x-portkey-'; 33 | export const PORTKEY_BASE_URL = 'https://api.portkey.ai/v1'; 34 | export const LOCAL_BASE_URL = 'http://localhost:8787/v1'; 35 | export const PORTKEY_GATEWAY_URL = PORTKEY_BASE_URL; 36 | 37 | export const PORTKEY_API_KEY_ENV = 'PORTKEY_API_KEY'; 38 | export const PORTKEY_PROXY_ENV = 'PORTKEY_PROXY'; 39 | 40 | export const OPEN_AI_API_KEY = 'DUMMY-KEY'; 41 | 42 | // API routes 43 | export const CHAT_COMPLETE_API = '/chat/completions'; 44 | export const TEXT_COMPLETE_API = '/completions'; 45 | export const PROMPT_API = '/prompt/complete'; 46 | export const FEEDBACK_API = '/feedback'; 47 | export const EMBEDDINGS_API = '/embeddings'; 48 | export const LOGS_API = '/logs'; 49 | export const PROMPTS_API = '/prompts'; 50 | export const PROMPT_PARTIALS_API = '/prompts/partials'; 51 | export const LABELS_API = '/labels'; 52 | export const COLLECTIONS_API = '/collections'; 53 | export const AUDIO_FILE_DURATION_HEADER = 'audio-file-duration'; 54 | -------------------------------------------------------------------------------- /src/core.ts: -------------------------------------------------------------------------------- 1 | type Browser = 'ie' | 'edge' | 'chrome' | 'firefox' | 'safari'; 2 | 3 | type BrowserInfo = { 4 | browser: Browser; 5 | version: string; 6 | }; 7 | 8 | export function getBrowserInfo(): BrowserInfo | null { 9 | if (typeof navigator === 'undefined' || !navigator) { 10 | return null; 11 | } 12 | 13 | // NOTE: The order matters here! 14 | const browserPatterns = [ 15 | { key: 'edge' as const, pattern: /Edge(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/ }, 16 | { key: 'ie' as const, pattern: /MSIE(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/ }, 17 | { 18 | key: 'ie' as const, 19 | // eslint-disable-next-line no-useless-escape 20 | pattern: /Trident(?:.*rv\:(\d+)\.(\d+)(?:\.(\d+))?)?/, 21 | }, 22 | { 23 | key: 'chrome' as const, 24 | pattern: /Chrome(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/, 25 | }, 26 | { 27 | key: 'firefox' as const, 28 | pattern: /Firefox(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/, 29 | }, 30 | { 31 | key: 'safari' as const, 32 | pattern: 33 | /(?:Version\W+(\d+)\.(\d+)(?:\.(\d+))?)?(?:\W+Mobile\S*)?\W+Safari/, 34 | }, 35 | ]; 36 | 37 | // Find the FIRST matching browser 38 | for (const { key, pattern } of browserPatterns) { 39 | const match = pattern.exec(navigator.userAgent); 40 | if (match) { 41 | const major = match[1] || 0; 42 | const minor = match[2] || 0; 43 | const patch = match[3] || 0; 44 | 45 | return { browser: key, version: `${major}.${minor}.${patch}` }; 46 | } 47 | } 48 | 49 | return null; 50 | } 51 | 52 | export const isRunningInBrowser = () => { 53 | return ( 54 | // @ts-ignore 55 | typeof window !== 'undefined' && 56 | // @ts-ignore 57 | typeof window.document !== 'undefined' && 58 | // @ts-ignore 59 | typeof navigator !== 'undefined' 60 | ); 61 | }; 62 | -------------------------------------------------------------------------------- /src/error.ts: -------------------------------------------------------------------------------- 1 | import { Headers } from './_types/generalTypes'; 2 | import { castToError } from './utils'; 3 | 4 | export class APIError extends Error { 5 | readonly status: number | undefined; 6 | readonly headers: Headers | undefined; 7 | readonly error: Object | undefined; 8 | 9 | constructor( 10 | status: number | undefined, 11 | error: Object | undefined, 12 | message: string | undefined, 13 | headers: Headers | undefined 14 | ) { 15 | super(APIError.makeMessage(error, message)); 16 | this.status = status; 17 | this.headers = headers; 18 | this.error = error; 19 | } 20 | 21 | private static makeMessage(error: any, message: string | undefined) { 22 | return error?.message 23 | ? typeof error.message === 'string' 24 | ? error.message 25 | : JSON.stringify(error.message) 26 | : error 27 | ? JSON.stringify(error) 28 | : message || 'Unknown error occurred'; 29 | } 30 | 31 | static generate( 32 | status: number | undefined, 33 | errorResponse: Object | undefined, 34 | message: string | undefined, 35 | headers: Headers | undefined 36 | ) { 37 | if (!status) { 38 | return new APIConnectionError({ cause: castToError(errorResponse) }); 39 | } 40 | 41 | const error = errorResponse as Record; 42 | 43 | if (status === 400) { 44 | return new BadRequestError(status, error, message, headers); 45 | } 46 | 47 | if (status === 401) { 48 | return new AuthenticationError(status, error, message, headers); 49 | } 50 | 51 | if (status === 403) { 52 | return new PermissionDeniedError(status, error, message, headers); 53 | } 54 | 55 | if (status === 404) { 56 | return new NotFoundError(status, error, message, headers); 57 | } 58 | 59 | if (status === 409) { 60 | return new ConflictError(status, error, message, headers); 61 | } 62 | 63 | if (status === 422) { 64 | return new UnprocessableEntityError(status, error, message, headers); 65 | } 66 | 67 | if (status === 429) { 68 | return new RateLimitError(status, error, message, headers); 69 | } 70 | 71 | if (status >= 500) { 72 | return new InternalServerError(status, error, message, headers); 73 | } 74 | 75 | return new APIError(status, error, message, headers); 76 | } 77 | } 78 | 79 | export class APIUserAbortError extends APIError { 80 | override readonly status: undefined = undefined; 81 | 82 | constructor({ message }: { message?: string } = {}) { 83 | super(undefined, undefined, message || 'Request was aborted.', undefined); 84 | } 85 | } 86 | 87 | export class APIConnectionError extends APIError { 88 | override readonly status: undefined = undefined; 89 | 90 | constructor({ 91 | message, 92 | cause, 93 | }: { 94 | message?: string; 95 | cause?: Error | undefined; 96 | }) { 97 | const LOCALHOST_CONNECTION_ERROR = `Could not instantiate the Portkey client. 98 | You can either add a valid 'apiKey' parameter (from https://app.portkey.ai/api-keys) 99 | or check the 'baseURL' parameter in the Portkey client, 100 | for your AI Gateway's instance's URL. \ 101 | 102 | CAUSE: ${cause} \ 103 | 104 | MESSAGE: ${message}`; 105 | super( 106 | undefined, 107 | undefined, 108 | message || LOCALHOST_CONNECTION_ERROR, 109 | undefined 110 | ); 111 | // in some environments the 'cause' property is already declared 112 | // @ts-ignore 113 | if (cause) this.cause = cause; 114 | } 115 | } 116 | 117 | export class APIConnectionTimeoutError extends APIConnectionError { 118 | constructor({ message }: { message?: string } = {}) { 119 | super({ message: message ?? 'Request timed out.' }); 120 | } 121 | } 122 | 123 | export class BadRequestError extends APIError { 124 | override readonly status: 400 = 400; 125 | } 126 | 127 | export class AuthenticationError extends APIError { 128 | override readonly status: 401 = 401; 129 | } 130 | 131 | export class PermissionDeniedError extends APIError { 132 | override readonly status: 403 = 403; 133 | } 134 | 135 | export class NotFoundError extends APIError { 136 | override readonly status: 404 = 404; 137 | } 138 | 139 | export class ConflictError extends APIError { 140 | override readonly status: 409 = 409; 141 | } 142 | 143 | export class UnprocessableEntityError extends APIError { 144 | override readonly status: 422 = 422; 145 | } 146 | 147 | export class RateLimitError extends APIError { 148 | override readonly status: 429 = 429; 149 | } 150 | 151 | export class InternalServerError extends APIError {} 152 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import * as apis from './apis'; 2 | import * as client from './client'; 3 | import * as consts from './constants'; 4 | import { PortkeyAIRealtimeWS } from './beta/realtime'; 5 | 6 | export import Portkey = client.Portkey; 7 | export import PORTKEY_GATEWAY_URL = consts.PORTKEY_GATEWAY_URL; 8 | export import createHeaders = apis.createHeaders; 9 | export { PortkeyAIRealtimeWS }; 10 | export * as types from './_types'; 11 | 12 | export default Portkey; 13 | -------------------------------------------------------------------------------- /src/streaming.ts: -------------------------------------------------------------------------------- 1 | import { Fetch } from './baseClient'; 2 | import { APIError } from './error'; 3 | 4 | type Bytes = string | ArrayBuffer | Uint8Array | Buffer | null | undefined; 5 | 6 | type ServerSentEvent = { 7 | event: string | null; 8 | data: string; 9 | raw: string[]; 10 | }; 11 | 12 | export const safeJSON = (text: string) => { 13 | try { 14 | return JSON.parse(text); 15 | } catch (err) { 16 | return undefined; 17 | } 18 | }; 19 | 20 | export const createResponseHeaders = ( 21 | headers: Awaited>['headers'] 22 | ): Record => { 23 | return new Proxy( 24 | Object.fromEntries( 25 | // @ts-ignore 26 | headers.entries() 27 | ), 28 | { 29 | get(target, name) { 30 | const key = name.toString(); 31 | return target[key.toLowerCase()] || target[key]; 32 | }, 33 | } 34 | ); 35 | }; 36 | 37 | export class Stream implements AsyncIterable { 38 | private response: Response; 39 | private decoder: SSEDecoder; 40 | 41 | constructor(response: Response) { 42 | this.response = response; 43 | this.decoder = new SSEDecoder(); 44 | } 45 | 46 | private async *iterMessages(): AsyncGenerator< 47 | ServerSentEvent, 48 | void, 49 | unknown 50 | > { 51 | if (!this.response.body) { 52 | throw new Error('Attempted to iterate over a response with no body'); 53 | } 54 | const lineDecoder = new LineDecoder(); 55 | 56 | const iter = readableStreamAsyncIterable(this.response.body); 57 | for await (const chunk of iter) { 58 | for (const line of lineDecoder.decode(chunk)) { 59 | const sse = this.decoder.decode(line); 60 | if (sse) yield sse; 61 | } 62 | } 63 | 64 | for (const line of lineDecoder.flush()) { 65 | const sse = this.decoder.decode(line); 66 | if (sse) yield sse; 67 | } 68 | } 69 | 70 | async *[Symbol.asyncIterator](): AsyncIterator { 71 | try { 72 | for await (const sse of this.iterMessages()) { 73 | if (sse.data.startsWith('[DONE]')) { 74 | continue; 75 | } 76 | if (sse.event === null) { 77 | try { 78 | yield JSON.parse(sse.data); 79 | } catch (e) { 80 | console.error('Could not parse message into JSON:', sse.data); 81 | console.error('From chunk:', sse.raw); 82 | throw e; 83 | } 84 | } 85 | 86 | if (sse.event === 'ping') { 87 | continue; 88 | } 89 | 90 | if (sse.event === 'error') { 91 | throw APIError; 92 | } 93 | } 94 | } catch (e) { 95 | if (e instanceof Error && e.name === 'AbortError') return; 96 | throw e; 97 | } 98 | } 99 | } 100 | 101 | class SSEDecoder { 102 | private data: string[]; 103 | private event: string | null; 104 | private chunks: string[]; 105 | 106 | constructor() { 107 | this.event = null; 108 | this.data = []; 109 | this.chunks = []; 110 | } 111 | 112 | decode(line: string) { 113 | if (line.endsWith('\r')) { 114 | line = line.substring(0, line.length - 1); 115 | } 116 | 117 | if (!line) { 118 | // empty line and we didn't previously encounter any messages 119 | if (!this.event && !this.data.length) return null; 120 | 121 | const sse: ServerSentEvent = { 122 | event: this.event, 123 | data: this.data.join('\n'), 124 | raw: this.chunks, 125 | }; 126 | 127 | this.event = null; 128 | this.data = []; 129 | this.chunks = []; 130 | 131 | return sse; 132 | } 133 | 134 | this.chunks.push(line); 135 | 136 | if (line.startsWith(':')) { 137 | return null; 138 | } 139 | 140 | let [fieldname, _, value] = partition(line, ':'); 141 | 142 | if (value.startsWith(' ')) { 143 | value = value.substring(1); 144 | } 145 | 146 | if (fieldname === 'event') { 147 | this.event = value; 148 | } else if (fieldname === 'data') { 149 | this.data.push(value); 150 | } 151 | 152 | return null; 153 | } 154 | } 155 | 156 | /** 157 | * A re-implementation of httpx's `LineDecoder` in Python that handles incrementally 158 | * reading lines from text. 159 | * 160 | * https://github.com/encode/httpx/blob/920333ea98118e9cf617f246905d7b202510941c/httpx/_decoders.py#L258 161 | */ 162 | class LineDecoder { 163 | // prettier-ignore 164 | static NEWLINE_CHARS = new Set(["\n", "\r", "\x0b", "\x0c", "\x1c", "\x1d", "\x1e", "\x85", "\u2028", "\u2029"]); 165 | static NEWLINE_REGEXP = /\r\n|[\n\r\x0b\x0c\x1c\x1d\x1e\x85\u2028\u2029]/g; 166 | 167 | buffer: string[]; 168 | trailingCR: boolean; 169 | textDecoder: any; // TextDecoder found in browsers; not typed to avoid pulling in either "dom" or "node" types. 170 | 171 | constructor() { 172 | this.buffer = []; 173 | this.trailingCR = false; 174 | } 175 | 176 | decode(chunk: Bytes): string[] { 177 | let text = this.decodeText(chunk); 178 | 179 | if (this.trailingCR) { 180 | text = '\r' + text; 181 | this.trailingCR = false; 182 | } 183 | if (text.endsWith('\r')) { 184 | this.trailingCR = true; 185 | text = text.slice(0, -1); 186 | } 187 | 188 | if (!text) { 189 | return []; 190 | } 191 | 192 | const trailingNewline = LineDecoder.NEWLINE_CHARS.has( 193 | text[text.length - 1] || '' 194 | ); 195 | let lines = text.split(LineDecoder.NEWLINE_REGEXP); 196 | 197 | if (lines.length === 1 && !trailingNewline) { 198 | this.buffer.push(lines[0]!); 199 | return []; 200 | } 201 | 202 | if (this.buffer.length > 0) { 203 | lines = [this.buffer.join('') + lines[0], ...lines.slice(1)]; 204 | this.buffer = []; 205 | } 206 | 207 | if (!trailingNewline) { 208 | this.buffer = [lines.pop() || '']; 209 | } 210 | 211 | return lines; 212 | } 213 | 214 | decodeText(bytes: Bytes): string { 215 | if (bytes == null) return ''; 216 | if (typeof bytes === 'string') return bytes; 217 | 218 | // Node: 219 | if (typeof Buffer !== 'undefined') { 220 | if (bytes instanceof Buffer) { 221 | return bytes.toString(); 222 | } 223 | if (bytes instanceof Uint8Array) { 224 | return Buffer.from(bytes).toString(); 225 | } 226 | 227 | throw new Error( 228 | `Unexpected: received non-Uint8Array (${bytes.constructor.name}) stream chunk in an environment with a global "Buffer" defined, which this library assumes to be Node. Please report this error.` 229 | ); 230 | } 231 | 232 | // Browser 233 | if (typeof TextDecoder !== 'undefined') { 234 | if (bytes instanceof Uint8Array || bytes instanceof ArrayBuffer) { 235 | this.textDecoder ??= new TextDecoder('utf8'); 236 | return this.textDecoder.decode(bytes); 237 | } 238 | 239 | throw new Error( 240 | `Unexpected: received non-Uint8Array/ArrayBuffer (${ 241 | (bytes as any).constructor.name 242 | }) in a web platform. Please report this error.` 243 | ); 244 | } 245 | 246 | throw new Error( 247 | 'Unexpected: neither Buffer nor TextDecoder are available as globals. Please report this error.' 248 | ); 249 | } 250 | 251 | flush(): string[] { 252 | if (!this.buffer.length && !this.trailingCR) { 253 | return []; 254 | } 255 | 256 | const lines = [this.buffer.join('')]; 257 | this.buffer = []; 258 | this.trailingCR = false; 259 | return lines; 260 | } 261 | } 262 | 263 | function partition(str: string, delimiter: string): [string, string, string] { 264 | const index = str.indexOf(delimiter); 265 | if (index !== -1) { 266 | return [ 267 | str.substring(0, index), 268 | delimiter, 269 | str.substring(index + delimiter.length), 270 | ]; 271 | } 272 | 273 | return [str, '', '']; 274 | } 275 | 276 | /** 277 | * Most browsers don't yet have async iterable support for ReadableStream, 278 | * and Node has a very different way of reading bytes from its "ReadableStream". 279 | * 280 | * This polyfill was pulled from https://github.com/MattiasBuelens/web-streams-polyfill/pull/122#issuecomment-1627354490 281 | */ 282 | function readableStreamAsyncIterable(stream: any): AsyncIterableIterator { 283 | if (stream[Symbol.asyncIterator]) return stream; 284 | 285 | const reader = stream.getReader(); 286 | return { 287 | async next() { 288 | try { 289 | const result = await reader.read(); 290 | if (result?.done) reader.releaseLock(); // release lock when stream becomes closed 291 | return result; 292 | } catch (e) { 293 | reader.releaseLock(); // release lock when stream becomes errored 294 | throw e; 295 | } 296 | }, 297 | async return() { 298 | const cancelPromise = reader.cancel(); 299 | reader.releaseLock(); 300 | await cancelPromise; 301 | return { done: true, value: undefined }; 302 | }, 303 | [Symbol.asyncIterator]() { 304 | return this; 305 | }, 306 | }; 307 | } 308 | -------------------------------------------------------------------------------- /src/utils.ts: -------------------------------------------------------------------------------- 1 | import { 2 | LOCAL_BASE_URL, 3 | MISSING_API_KEY_ERROR_MESSAGE, 4 | OPEN_AI_API_KEY, 5 | PORTKEY_BASE_URL, 6 | PORTKEY_HEADER_PREFIX, 7 | } from './constants'; 8 | import { createResponseHeaders } from './streaming'; 9 | import OpenAI from 'openai'; 10 | import type { Portkey } from './index'; 11 | import { 12 | UserInviteListParams, 13 | UsersListParams, 14 | WorkspaceMemberListParams, 15 | WorkspacesListParams, 16 | } from './apis/admin'; 17 | import { VirtualKeysListParams } from './apis/virtualKeys'; 18 | import { ApiKeysListParams } from './apis/apiKeys'; 19 | import { CongfigsListParams } from './apis/configs'; 20 | import { LogsExportListParams } from './apis/logsExport'; 21 | import { getBrowserInfo } from './core'; 22 | 23 | type PlatformProperties = { 24 | 'x-portkey-runtime'?: string; 25 | 'x-portkey-runtime-version'?: string; 26 | }; 27 | export const getPlatformProperties = (): PlatformProperties => { 28 | if ( 29 | Object.prototype.toString.call( 30 | typeof process !== 'undefined' ? process : 0 31 | ) === '[object process]' 32 | ) { 33 | return { 34 | [`${PORTKEY_HEADER_PREFIX}runtime`]: 'node', 35 | [`${PORTKEY_HEADER_PREFIX}runtime-version`]: process.version, 36 | }; 37 | } 38 | 39 | const browserInfo = getBrowserInfo(); 40 | if (browserInfo) { 41 | return { 42 | [`${PORTKEY_HEADER_PREFIX}runtime`]: `browser: ${browserInfo.browser}`, 43 | [`${PORTKEY_HEADER_PREFIX}runtime-version`]: browserInfo.version, 44 | }; 45 | } 46 | return {}; 47 | }; 48 | 49 | export const readEnv = (env: string): string | undefined => { 50 | if (typeof process !== 'undefined') { 51 | return process.env?.[env] ?? undefined; 52 | } 53 | return undefined; 54 | }; 55 | 56 | export const castToError = (err: any): Error => { 57 | if (err instanceof Error) return err; 58 | return new Error(err); 59 | }; 60 | 61 | export const isEmpty = (value: unknown) => { 62 | // Check if the value is null or undefined 63 | if (value == null) { 64 | return true; 65 | } 66 | 67 | // Check if the value is a string and has zero length 68 | if (typeof value === 'string' && value.trim().length === 0) { 69 | return true; 70 | } 71 | 72 | // Check if the value is an array and has zero elements 73 | if (Array.isArray(value) && value.length === 0) { 74 | return true; 75 | } 76 | 77 | // Check if the value is an object and has zero keys 78 | if (typeof value === 'object' && Object.keys(value).length === 0) { 79 | return true; 80 | } 81 | 82 | // If none of the above conditions are met, the value is not empty 83 | return false; 84 | }; 85 | 86 | export const getPortkeyHeader = (key: string): string => { 87 | return `${PORTKEY_HEADER_PREFIX}${key}`; 88 | }; 89 | 90 | type Config = Record | string | null | undefined; 91 | 92 | export const overrideConfig = ( 93 | initialConfig?: Config, 94 | updatedConfig?: Config 95 | ): Config => { 96 | if (isEmpty(updatedConfig)) { 97 | return initialConfig; 98 | } 99 | return updatedConfig; 100 | }; 101 | 102 | export const parseBody = ( 103 | data: Record | undefined | null 104 | ): Record => { 105 | // Making sure that every key in the body is in snake case 106 | if (isEmpty(data)) { 107 | return {}; 108 | } 109 | const parsedData: Record = {}; 110 | for (let k in data) { 111 | const v = data[k]; 112 | // convert to snakecase 113 | k = k 114 | .replace('ID', 'Id') 115 | .replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`); 116 | 117 | parsedData[k] = v; 118 | } 119 | return parsedData; 120 | }; 121 | 122 | export function finalResponse(response: any) { 123 | const headers = portkeyHeaders(response.response.headers); 124 | const json = { 125 | ...(response.data?.body || response.data), 126 | getHeaders: () => headers, 127 | }; 128 | return json; 129 | } 130 | 131 | export function portkeyHeaders(headers: any) { 132 | const parsedHeaders = createResponseHeaders(headers); 133 | const prefix = PORTKEY_HEADER_PREFIX; 134 | const filteredHeaders = Object.entries(parsedHeaders) 135 | .filter(([key, _]) => key.startsWith(prefix)) // eslint-disable-line @typescript-eslint/no-unused-vars 136 | .map(([key, value]) => [key.replace(prefix, ''), value]); 137 | 138 | return Object.fromEntries(filteredHeaders); 139 | } 140 | 141 | export function defaultHeadersBuilder(client: any) { 142 | const customHeaders = client.customHeaders; 143 | const portkeyHeaders = client.portkeyHeaders; 144 | 145 | // Logic to add Bearer only if it is not present. 146 | // Else it would be added everytime a request is made 147 | if ( 148 | Object.prototype.hasOwnProperty.call(customHeaders, 'authorization') && 149 | !customHeaders['authorization'].startsWith('Bearer') 150 | ) { 151 | client.customHeaders['authorization'] = 152 | 'Bearer ' + client.customHeaders['authorization']; 153 | } 154 | 155 | return { ...customHeaders, ...portkeyHeaders }; 156 | } 157 | 158 | export function initOpenAIClient(client: Portkey) { 159 | return new OpenAI({ 160 | apiKey: client.apiKey || readEnv('OPENAI_API_KEY') || OPEN_AI_API_KEY, 161 | baseURL: client.baseURL, 162 | defaultHeaders: defaultHeadersBuilder(client), 163 | maxRetries: 0, 164 | dangerouslyAllowBrowser: client.dangerouslyAllowBrowser ?? false, 165 | fetch: async (url: RequestInfo, init?: RequestInit): Promise => { 166 | // NOTE: For adding duplex option only when body is a Readable stream 167 | const fetchOptions: RequestInit = { 168 | ...init, 169 | ...(init?.body && 170 | typeof (init.body as any)?.pipe === 'function' && { duplex: 'half' }), 171 | }; 172 | 173 | let isRetrying = false; 174 | let response: Response | undefined; 175 | try { 176 | response = await fetch(url, fetchOptions); 177 | } catch (error) { 178 | isRetrying = true; 179 | } 180 | if ( 181 | isRetrying || 182 | (response && !response.ok && client._shouldRetry(response)) 183 | ) { 184 | return await fetch(url, fetchOptions); 185 | } else if (response) { 186 | return response; 187 | } else { 188 | throw castToError(response); 189 | } 190 | }, 191 | }); 192 | } 193 | 194 | export function toQueryParams( 195 | params?: 196 | | UsersListParams 197 | | UserInviteListParams 198 | | WorkspacesListParams 199 | | WorkspaceMemberListParams 200 | | VirtualKeysListParams 201 | | ApiKeysListParams 202 | | CongfigsListParams 203 | | LogsExportListParams 204 | | any 205 | ): string { 206 | if (!params) { 207 | return ''; 208 | } 209 | const queryParams = Object.entries(params) 210 | .filter(([, value]) => value !== undefined && value !== null) 211 | .map(([key, value]) => `${key}=${value}`) 212 | .join('&'); 213 | 214 | return queryParams ? `?${queryParams}` : ''; 215 | } 216 | 217 | export function setBaseURL(baseURL: any, apiKey: any) { 218 | if (baseURL) { 219 | return baseURL; 220 | } 221 | return apiKey ? PORTKEY_BASE_URL : LOCAL_BASE_URL; 222 | } 223 | 224 | export function setApiKey(baseURL: any, apiKey: any) { 225 | if (apiKey) { 226 | return apiKey; 227 | } 228 | if (baseURL === PORTKEY_BASE_URL && !apiKey) { 229 | throw castToError(MISSING_API_KEY_ERROR_MESSAGE); 230 | } 231 | } 232 | -------------------------------------------------------------------------------- /src/version.ts: -------------------------------------------------------------------------------- 1 | export const VERSION = '1.9.1'; 2 | -------------------------------------------------------------------------------- /tests/assistants/openai.test.ts: -------------------------------------------------------------------------------- 1 | import { config } from 'dotenv'; 2 | import { Portkey } from 'portkey-ai'; 3 | 4 | config({ override: true }) 5 | const client = new Portkey({ 6 | apiKey: process.env["PORTKEY_API_KEY"] ?? "", 7 | virtualKey: process.env["OPENAI_VIRTUAL_KEY"] ?? "" 8 | }); 9 | 10 | describe('OpenAI Assistants APIs', () => { 11 | test('assistant: create: documentation', async () => { 12 | const myAssistant = await client.beta.assistants.create({ 13 | instructions: 14 | "You are a personal math tutor. When asked a question, write and run Python code to answer the question.", 15 | name: "Math Tutor", 16 | tools: [{ type: "code_interpreter" }], 17 | model: "gpt-4", 18 | }); 19 | expect(myAssistant).toBeDefined(); 20 | expect(myAssistant.tools).toBeDefined(); 21 | expect(myAssistant.tools.length).toBeGreaterThan(0); 22 | }); 23 | 24 | }); -------------------------------------------------------------------------------- /tests/audio/openai.test.ts: -------------------------------------------------------------------------------- 1 | import { config } from "dotenv"; 2 | import { Portkey } from "portkey-ai"; 3 | import fs from "fs"; 4 | import path from "path"; 5 | 6 | config({ override: true }); 7 | const client = new Portkey({ 8 | apiKey: process.env["PORTKEY_API_KEY"] ?? "", 9 | virtualKey: process.env["OPENAI_VIRTUAL_KEY"] ?? "" 10 | }); 11 | 12 | describe("Openai Audio APIs", () => { 13 | test("Speech: only required params", async () => { 14 | 15 | const speechFile = path.resolve("./speech.mp3"); 16 | const response = await client.audio.speech.create({ 17 | model:"tts-1", 18 | voice:"alloy", 19 | input:"The quick brown fox jumps over the lazy dog" 20 | }); 21 | const buffer = Buffer.from(await response.arrayBuffer()); 22 | await fs.promises.writeFile(speechFile, buffer); 23 | expect(response).toBeDefined(); 24 | }); 25 | 26 | 27 | test("Transcription: only required params", async () => { 28 | const transcription = await client.audio.transcriptions.create({ 29 | file: fs.createReadStream("./speech.mp3"), 30 | model: "whisper-1", 31 | }); 32 | expect(transcription).toBeDefined(); 33 | expect(transcription.text).toBeDefined(); 34 | }); 35 | 36 | test("Translation: only required params", async () => { 37 | const transcription = await client.audio.translations.create({ 38 | file: fs.createReadStream("./speech.mp3"), 39 | model: "whisper-1", 40 | }); 41 | expect(transcription).toBeDefined(); 42 | expect(transcription.text).toBeDefined(); 43 | }); 44 | 45 | }); 46 | -------------------------------------------------------------------------------- /tests/audio/speech.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Portkey-AI/portkey-node-sdk/d18d452e9eda254f993cc01c0a8b67ad8a5eb812/tests/audio/speech.mp3 -------------------------------------------------------------------------------- /tests/chat/anthropic.test.ts: -------------------------------------------------------------------------------- 1 | import { config } from 'dotenv'; 2 | import { Portkey } from 'portkey-ai'; 3 | 4 | config({ override: true }) 5 | const client = new Portkey({ 6 | apiKey: process.env["PORTKEY_API_KEY"] ?? "", 7 | virtualKey: process.env["ANTHROPIC_VIRTUAL_KEY"] ?? "" 8 | }); 9 | 10 | describe('Anthropic ChatCompletions APIs', () => { 11 | test('model: claude-3-opus-20240229', async () => { 12 | const completion = await client.chat.completions.create({ model: 'claude-3-opus-20240229', messages: [{ "role": "user", "content": "Say this is a test" }], max_tokens: 275 }); 13 | expect(completion).toBeDefined(); 14 | expect(completion.choices).toBeDefined(); 15 | expect(completion.choices.length).toBeGreaterThan(0); 16 | }); 17 | 18 | test('model: claude-3-sonnet-20240229', async () => { 19 | const completion = await client.chat.completions.create({ model: 'claude-3-sonnet-20240229', messages: [{ "role": "user", "content": "Say this is a test" }], max_tokens: 275 }); 20 | expect(completion).toBeDefined(); 21 | expect(completion.choices).toBeDefined(); 22 | expect(completion.choices.length).toBeGreaterThan(0); 23 | }); 24 | 25 | test('model: claude-3-haiku-20240307', async () => { 26 | const completion = await client.chat.completions.create({ model: 'claude-3-haiku-20240307', messages: [{ "role": "user", "content": "Say this is a test" }], max_tokens: 275 }); 27 | expect(completion).toBeDefined(); 28 | expect(completion.choices).toBeDefined(); 29 | expect(completion.choices.length).toBeGreaterThan(0); 30 | }); 31 | 32 | test('model: claude-2.1', async () => { 33 | const completion = await client.chat.completions.create({ model: 'claude-2.1', messages: [{ "role": "user", "content": "Say this is a test" }], max_tokens: 275 }); 34 | expect(completion).toBeDefined(); 35 | expect(completion.choices).toBeDefined(); 36 | expect(completion.choices.length).toBeGreaterThan(0); 37 | }); 38 | 39 | test('model: claude-2.0', async () => { 40 | const completion = await client.chat.completions.create({ model: 'claude-2.0', messages: [{ "role": "user", "content": "Say this is a test" }], max_tokens: 275 }); 41 | expect(completion).toBeDefined(); 42 | expect(completion.choices).toBeDefined(); 43 | expect(completion.choices.length).toBeGreaterThan(0); 44 | }); 45 | 46 | test('model: claude-instant-1.2', async () => { 47 | const completion = await client.chat.completions.create({ model: 'claude-instant-1.2', messages: [{ "role": "user", "content": "Say this is a test" }], max_tokens: 275 }); 48 | expect(completion).toBeDefined(); 49 | expect(completion.choices).toBeDefined(); 50 | expect(completion.choices.length).toBeGreaterThan(0); 51 | }); 52 | }); -------------------------------------------------------------------------------- /tests/chat/anyscale.test.ts: -------------------------------------------------------------------------------- 1 | import { config } from 'dotenv'; 2 | import { Portkey } from 'portkey-ai'; 3 | 4 | config({ override: true }) 5 | const client = new Portkey({ 6 | apiKey: process.env["PORTKEY_API_KEY"] ?? "", 7 | virtualKey: process.env["ANYSCALE_VIRTUAL_KEY"] ?? "" 8 | }); 9 | 10 | describe('Anyscale ChatCompletions APIs', () => { 11 | test('model: meta-llama/Llama-2-7b-chat-hf', async () => { 12 | const completion = await client.chat.completions.create({ model: 'meta-llama/Llama-2-7b-chat-hf', messages: [{ "role": "user", "content": "Say this is a test" }] }); 13 | expect(completion).toBeDefined(); 14 | expect(completion.choices).toBeDefined(); 15 | expect(completion.choices.length).toBeGreaterThan(0); 16 | }); 17 | 18 | test('model: meta-llama/Llama-2-13b-chat-hf', async () => { 19 | const completion = await client.chat.completions.create({ model: 'meta-llama/Llama-2-13b-chat-hf', messages: [{ "role": "user", "content": "Say this is a test" }] }); 20 | expect(completion).toBeDefined(); 21 | expect(completion.choices).toBeDefined(); 22 | expect(completion.choices.length).toBeGreaterThan(0); 23 | }); 24 | 25 | test('model: meta-llama/Llama-2-70b-chat-hf', async () => { 26 | const completion = await client.chat.completions.create({ model: 'meta-llama/Llama-2-70b-chat-hf', messages: [{ "role": "user", "content": "Say this is a test" }] }); 27 | expect(completion).toBeDefined(); 28 | expect(completion.choices).toBeDefined(); 29 | expect(completion.choices.length).toBeGreaterThan(0); 30 | }); 31 | 32 | test('model: codellama/CodeLlama-34b-Instruct-hf', async () => { 33 | const completion = await client.chat.completions.create({ 34 | model: 'codellama/CodeLlama-34b-Instruct-hf', 35 | messages: [{ "role": "user", "content": "Say this is a test" }], 36 | max_tokens: 30 37 | }); 38 | expect(completion).toBeDefined(); 39 | expect(completion.choices).toBeDefined(); 40 | expect(completion.choices.length).toBeGreaterThan(0); 41 | }); 42 | 43 | test('model: mistralai/Mistral-7B-Instruct-v0.1', async () => { 44 | const completion = await client.chat.completions.create({ model: 'mistralai/Mistral-7B-Instruct-v0.1', messages: [{ "role": "user", "content": "Say this is a test" }] }); 45 | expect(completion).toBeDefined(); 46 | expect(completion.choices).toBeDefined(); 47 | expect(completion.choices.length).toBeGreaterThan(0); 48 | }); 49 | 50 | test('model: google/gemma-7b-it', async () => { 51 | const completion = await client.chat.completions.create({ 52 | model: 'google/gemma-7b-it', 53 | messages: [{ "role": "user", "content": "Say this is a test" }], 54 | max_tokens: 25 55 | }); 56 | expect(completion).toBeDefined(); 57 | expect(completion.choices).toBeDefined(); 58 | expect(completion.choices.length).toBeGreaterThan(0); 59 | }); 60 | 61 | test('model: meta-llama/Meta-Llama-3-8B-Instruct', async () => { 62 | const completion = await client.chat.completions.create({ 63 | model: 'meta-llama/Meta-Llama-3-8B-Instruct', 64 | messages: [{ "role": "user", "content": "Say this is a test" }], 65 | max_tokens: 25 66 | }); 67 | expect(completion).toBeDefined(); 68 | expect(completion.choices).toBeDefined(); 69 | expect(completion.choices.length).toBeGreaterThan(0); 70 | }); 71 | 72 | test('model: meta-llama/Meta-Llama-3-70B-Instruct', async () => { 73 | const completion = await client.chat.completions.create({ 74 | model: 'meta-llama/Meta-Llama-3-70B-Instruct', 75 | messages: [{ role: 'user', content: 'Say this is a test' }], 76 | max_tokens: 25, 77 | }); 78 | expect(completion).toBeDefined(); 79 | expect(completion.choices).toBeDefined(); 80 | expect(completion.choices.length).toBeGreaterThan(0); 81 | }); 82 | 83 | test('model: mistralai/Mixtral-8x7B-Instruct-v0.1', async () => { 84 | const completion = await client.chat.completions.create({ 85 | model: 'mistralai/Mixtral-8x7B-Instruct-v0.1', 86 | messages: [{ role: 'user', content: 'Say this is a test' }], 87 | max_tokens: 25, 88 | }); 89 | expect(completion).toBeDefined(); 90 | expect(completion.choices).toBeDefined(); 91 | expect(completion.choices.length).toBeGreaterThan(0); 92 | }); 93 | 94 | test('model: mistralai/Mixtral-8x22B-Instruct-v0.1', async () => { 95 | const completion = await client.chat.completions.create({ 96 | model: 'mistralai/Mixtral-8x22B-Instruct-v0.1', 97 | messages: [{ role: 'user', content: 'Say this is a test' }], 98 | max_tokens: 25, 99 | }); 100 | expect(completion).toBeDefined(); 101 | expect(completion.choices).toBeDefined(); 102 | expect(completion.choices.length).toBeGreaterThan(0); 103 | }); 104 | 105 | test('model: mlabonne/NeuralHermes-2.5-Mistral-7B', async () => { 106 | const completion = await client.chat.completions.create({ 107 | model: 'mlabonne/NeuralHermes-2.5-Mistral-7B', 108 | messages: [{ role: 'user', content: 'Say this is a test' }], 109 | max_tokens: 25, 110 | }); 111 | expect(completion).toBeDefined(); 112 | expect(completion.choices).toBeDefined(); 113 | expect(completion.choices.length).toBeGreaterThan(0); 114 | }); 115 | }); -------------------------------------------------------------------------------- /tests/chat/openai.test.ts: -------------------------------------------------------------------------------- 1 | import { config } from 'dotenv'; 2 | import { Portkey } from 'portkey-ai'; 3 | 4 | config({ override: true }) 5 | const client = new Portkey({ 6 | apiKey: process.env["PORTKEY_API_KEY"] ?? "", 7 | virtualKey: process.env["OPENAI_VIRTUAL_KEY"] ?? "" 8 | }); 9 | 10 | describe('Openai ChatCompletions APIs', () => { 11 | test('model: gpt-4-0125-preview', async () => { 12 | const completion = await client.chat.completions.create({ model: 'gpt-4-0125-preview', messages: [{ "role": "user", "content": "Say this is a test" }] }); 13 | expect(completion).toBeDefined(); 14 | expect(completion.choices).toBeDefined(); 15 | expect(completion.choices.length).toBeGreaterThan(0); 16 | }); 17 | 18 | test('model: gpt-4-turbo-preview', async () => { 19 | const completion = await client.chat.completions.create({ model: 'gpt-4-turbo-preview', messages: [{ "role": "user", "content": "Say this is a test" }] }); 20 | expect(completion).toBeDefined(); 21 | expect(completion.choices).toBeDefined(); 22 | expect(completion.choices.length).toBeGreaterThan(0); 23 | }); 24 | 25 | test('model: gpt-4-1106-preview', async () => { 26 | const completion = await client.chat.completions.create({ model: 'gpt-4-1106-preview', messages: [{ "role": "user", "content": "Say this is a test" }] }); 27 | expect(completion).toBeDefined(); 28 | expect(completion.choices).toBeDefined(); 29 | expect(completion.choices.length).toBeGreaterThan(0); 30 | }); 31 | 32 | test('model: gpt-4-vision-preview', async () => { 33 | const completion = await client.chat.completions.create({ model: 'gpt-4-vision-preview', messages: [{ "role": "user", "content": "Say this is a test" }] }); 34 | expect(completion).toBeDefined(); 35 | expect(completion.choices).toBeDefined(); 36 | expect(completion.choices.length).toBeGreaterThan(0); 37 | }); 38 | 39 | test('model: gpt-4', async () => { 40 | const completion = await client.chat.completions.create({ model: 'gpt-4', messages: [{ "role": "user", "content": "Say this is a test" }] }); 41 | expect(completion).toBeDefined(); 42 | expect(completion.choices).toBeDefined(); 43 | expect(completion.choices.length).toBeGreaterThan(0); 44 | }); 45 | 46 | test('model: gpt-4-0613', async () => { 47 | const completion = await client.chat.completions.create({ model: 'gpt-4-0613', messages: [{ "role": "user", "content": "Say this is a test" }] }); 48 | expect(completion).toBeDefined(); 49 | expect(completion.choices).toBeDefined(); 50 | expect(completion.choices.length).toBeGreaterThan(0); 51 | }); 52 | 53 | test('model: gpt-3.5-turbo', async () => { 54 | const completion = await client.chat.completions.create({ model: 'gpt-3.5-turbo', messages: [{ "role": "user", "content": "Say this is a test" }] }); 55 | expect(completion).toBeDefined(); 56 | expect(completion.choices).toBeDefined(); 57 | expect(completion.choices.length).toBeGreaterThan(0); 58 | }); 59 | 60 | test('model: gpt-3.5-turbo-0125', async () => { 61 | const completion = await client.chat.completions.create({ model: 'gpt-3.5-turbo-0125', messages: [{ "role": "user", "content": "Say this is a test" }] }); 62 | expect(completion).toBeDefined(); 63 | expect(completion.choices).toBeDefined(); 64 | expect(completion.choices.length).toBeGreaterThan(0); 65 | }); 66 | 67 | test('model: gpt-3.5-turbo-1106', async () => { 68 | const completion = await client.chat.completions.create({ model: 'gpt-3.5-turbo-1106', messages: [{ "role": "user", "content": "Say this is a test" }] }); 69 | expect(completion).toBeDefined(); 70 | expect(completion.choices).toBeDefined(); 71 | expect(completion.choices.length).toBeGreaterThan(0); 72 | }); 73 | 74 | test('model: gpt-4-turbo-2024-04-09', async () => { 75 | const completion = await client.chat.completions.create({ model: 'gpt-4-turbo-2024-04-09', messages: [{ "role": "user", "content": "Say this is a test" }] }); 76 | expect(completion).toBeDefined(); 77 | expect(completion.choices).toBeDefined(); 78 | expect(completion.choices.length).toBeGreaterThan(0); 79 | }); 80 | }); -------------------------------------------------------------------------------- /tests/completion/anthropic.test.ts: -------------------------------------------------------------------------------- 1 | import { config } from 'dotenv'; 2 | import { Portkey } from 'portkey-ai'; 3 | 4 | config({ override: true }) 5 | const client = new Portkey({ 6 | apiKey: process.env["PORTKEY_API_KEY"] ?? "", 7 | virtualKey: process.env["ANTHROPIC_VIRTUAL_KEY"] ?? "" 8 | }); 9 | 10 | describe('Anthropic ChatCompletions APIs', () => { 11 | test('model: claude-2.1', async () => { 12 | const completion = await client.chat.completions.create({ model: 'claude-2.1', messages: [{ "role": "user", "content": "Say this is a test" }], max_tokens: 275 }); 13 | expect(completion).toBeDefined(); 14 | expect(completion.choices).toBeDefined(); 15 | expect(completion.choices.length).toBeGreaterThan(0); 16 | }); 17 | 18 | test('model: claude-2.0', async () => { 19 | const completion = await client.chat.completions.create({ model: 'claude-2.0', messages: [{ "role": "user", "content": "Say this is a test" }], max_tokens: 275 }); 20 | expect(completion).toBeDefined(); 21 | expect(completion.choices).toBeDefined(); 22 | expect(completion.choices.length).toBeGreaterThan(0); 23 | }); 24 | 25 | test('model: claude-instant-1.2', async () => { 26 | const completion = await client.chat.completions.create({ model: 'claude-instant-1.2', messages: [{ "role": "user", "content": "Say this is a test" }], max_tokens: 275 }); 27 | expect(completion).toBeDefined(); 28 | expect(completion.choices).toBeDefined(); 29 | expect(completion.choices.length).toBeGreaterThan(0); 30 | }); 31 | }); -------------------------------------------------------------------------------- /tests/completion/anyscale.test.ts: -------------------------------------------------------------------------------- 1 | import { config } from 'dotenv'; 2 | import { Portkey } from 'portkey-ai'; 3 | 4 | config({ override: true }) 5 | const client = new Portkey({ 6 | apiKey: process.env["PORTKEY_API_KEY"] ?? "", 7 | virtualKey: process.env["ANYSCALE_VIRTUAL_KEY"] ?? "" 8 | }); 9 | 10 | describe('Anyscale ChatCompletions APIs', () => { 11 | test('model: meta-llama/Llama-2-7b-chat-hf', async () => { 12 | const completion = await client.chat.completions.create({ model: 'meta-llama/Llama-2-7b-chat-hf', messages: [{ "role": "user", "content": "Say this is a test" }] }); 13 | expect(completion).toBeDefined(); 14 | expect(completion.choices).toBeDefined(); 15 | expect(completion.choices.length).toBeGreaterThan(0); 16 | }); 17 | 18 | test('model: meta-llama/Llama-2-13b-chat-hf', async () => { 19 | const completion = await client.chat.completions.create({ model: 'meta-llama/Llama-2-13b-chat-hf', messages: [{ "role": "user", "content": "Say this is a test" }] }); 20 | expect(completion).toBeDefined(); 21 | expect(completion.choices).toBeDefined(); 22 | expect(completion.choices.length).toBeGreaterThan(0); 23 | }); 24 | 25 | test('model: meta-llama/Llama-2-70b-chat-hf', async () => { 26 | const completion = await client.chat.completions.create({ model: 'meta-llama/Llama-2-70b-chat-hf', messages: [{ "role": "user", "content": "Say this is a test" }] }); 27 | expect(completion).toBeDefined(); 28 | expect(completion.choices).toBeDefined(); 29 | expect(completion.choices.length).toBeGreaterThan(0); 30 | }); 31 | 32 | test('model: codellama/CodeLlama-34b-Instruct-hf', async () => { 33 | const completion = await client.chat.completions.create({ model: 'codellama/CodeLlama-34b-Instruct-hf', messages: [{ "role": "user", "content": "Say this is a test" }] }); 34 | expect(completion).toBeDefined(); 35 | expect(completion.choices).toBeDefined(); 36 | expect(completion.choices.length).toBeGreaterThan(0); 37 | }); 38 | 39 | test('model: mistralai/Mistral-7B-Instruct-v0.1', async () => { 40 | const completion = await client.chat.completions.create({ model: 'mistralai/Mistral-7B-Instruct-v0.1', messages: [{ "role": "user", "content": "Say this is a test" }] }); 41 | expect(completion).toBeDefined(); 42 | expect(completion.choices).toBeDefined(); 43 | expect(completion.choices.length).toBeGreaterThan(0); 44 | }); 45 | }); -------------------------------------------------------------------------------- /tests/completion/openai.test.ts: -------------------------------------------------------------------------------- 1 | import { config } from 'dotenv'; 2 | import { Portkey } from 'portkey-ai'; 3 | 4 | config({ override: true }) 5 | const client = new Portkey({ 6 | apiKey: process.env["PORTKEY_API_KEY"] ?? "", 7 | virtualKey: process.env["OPENAI_VIRTUAL_KEY"] ?? "" 8 | }); 9 | 10 | describe('Completions APIs', () => { 11 | test('model: gpt-3.5-turbo-instruct', async () => { 12 | const completion = await client.completions.create({ model: 'gpt-3.5-turbo-instruct', prompt: 'This is a test.' }); 13 | expect(completion).toBeDefined(); 14 | expect(completion.choices).toBeDefined(); 15 | expect(completion.choices.length).toBeGreaterThan(0); 16 | }); 17 | }); -------------------------------------------------------------------------------- /tests/images/image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Portkey-AI/portkey-node-sdk/d18d452e9eda254f993cc01c0a8b67ad8a5eb812/tests/images/image.png -------------------------------------------------------------------------------- /tests/images/imageMask.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Portkey-AI/portkey-node-sdk/d18d452e9eda254f993cc01c0a8b67ad8a5eb812/tests/images/imageMask.png -------------------------------------------------------------------------------- /tests/images/openai.test.ts: -------------------------------------------------------------------------------- 1 | import { config } from "dotenv"; 2 | import { Portkey } from "portkey-ai"; 3 | import fs from "fs"; 4 | import path from "path"; 5 | 6 | config({ override: true }); 7 | const client = new Portkey({ 8 | apiKey: process.env["PORTKEY_API_KEY"] ?? "", 9 | virtualKey: process.env["OPENAI_VIRTUAL_KEY"] ?? "" 10 | }); 11 | 12 | describe("Openai Images APIs", () => { 13 | test("generate: only required params", async () => { 14 | const response = await client.images.generate({ 15 | prompt: "A cute baby sea otter", 16 | }); 17 | expect(response).toBeDefined(); 18 | expect(response.data).toBeDefined(); 19 | expect(response.data.length).toBeGreaterThan(0); 20 | }, 120000); 21 | 22 | test("generate: only required params with model", async () => { 23 | const response = await client.images.generate({ 24 | model: "dall-e-3", 25 | prompt: "A cute baby sea otter", 26 | }); 27 | expect(response).toBeDefined(); 28 | expect(response.data).toBeDefined(); 29 | expect(response.data.length).toBeGreaterThan(0); 30 | }, 120000); 31 | 32 | test("createVariation: only required params", async () => { 33 | const imagePath = path.join(__dirname, 'image.png'); 34 | const response = await client.images.createVariation({ 35 | image: fs.createReadStream(imagePath), 36 | }); 37 | expect(response).toBeDefined(); 38 | expect(response.data).toBeDefined(); 39 | expect(response.data.length).toBeGreaterThan(0); 40 | }, 120000); 41 | 42 | test("edit: only required params", async () => { 43 | const imagePath = path.join(__dirname, 'image.png'); 44 | const imageMaskPath = path.join(__dirname, 'imageMask.png'); 45 | const response = await client.images.edit({ 46 | image: fs.createReadStream(imagePath), 47 | mask: fs.createReadStream(imageMaskPath), 48 | prompt:"A cute baby sea otter wearing a beret" 49 | }); 50 | expect(response).toBeDefined(); 51 | expect(response.data).toBeDefined(); 52 | expect(response.data.length).toBeGreaterThan(0); 53 | }, 120000); 54 | 55 | }); 56 | -------------------------------------------------------------------------------- /tests/moderations/openai.test.ts: -------------------------------------------------------------------------------- 1 | import { config } from 'dotenv'; 2 | import { Portkey } from 'portkey-ai'; 3 | 4 | config({ override: true }) 5 | const client = new Portkey({ 6 | apiKey: process.env["PORTKEY_API_KEY"] ?? "", 7 | virtualKey: process.env["OPENAI_VIRTUAL_KEY"] ?? "" 8 | }); 9 | 10 | describe('OpenAI Moderations APIs', () => { 11 | test('assistant: create: documentation', async () => { 12 | const moderation = await client.moderations.create({ 13 | input: "I want to kill them.", 14 | model: "text-moderation-stable", 15 | }); 16 | expect(moderation).toBeDefined(); 17 | expect(moderation.id).toBeDefined(); 18 | expect(moderation.results.length).toBeGreaterThan(0); 19 | }); 20 | 21 | }); -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "include": [ 3 | "src", 4 | "tests", 5 | "examples", 6 | ], 7 | "compilerOptions": { 8 | "sourceMap": true, 9 | "declaration": true, 10 | "target": "ES2015", 11 | "lib": [ 12 | "ES2015", 13 | "dom" 14 | ], 15 | "module": "CommonJS", 16 | "esModuleInterop": true, 17 | "baseUrl": ".", 18 | "paths": { 19 | "portkey-ai/*": [ 20 | "src/*" 21 | ], 22 | "portkey-ai": [ 23 | "src/index.ts" 24 | ], 25 | }, 26 | "resolveJsonModule": true, 27 | "strict": true, 28 | "noImplicitAny": true, 29 | "strictNullChecks": true, 30 | "strictFunctionTypes": true, 31 | "strictBindCallApply": true, 32 | "strictPropertyInitialization": true, 33 | "noImplicitThis": true, 34 | "alwaysStrict": true, 35 | "exactOptionalPropertyTypes": true, 36 | "noUncheckedIndexedAccess": true, 37 | "noImplicitOverride": true, 38 | "noPropertyAccessFromIndexSignature": true, 39 | "skipLibCheck": true, 40 | "outDir": "./dist", 41 | }, 42 | "exclude": [ 43 | "./node_modules" // Files/folders to exclude from compilation 44 | ], 45 | "jest": { 46 | "preset": "ts-jest" 47 | } 48 | } --------------------------------------------------------------------------------