Skip to content

Commit

Permalink
Add initial anthropic ai provider integration package (#4446)
Browse files Browse the repository at this point in the history
  • Loading branch information
IMax153 authored Feb 14, 2025
1 parent b56a211 commit 9375c28
Show file tree
Hide file tree
Showing 33 changed files with 4,787 additions and 1,111 deletions.
7 changes: 7 additions & 0 deletions .changeset/thin-hounds-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@effect/ai-anthropic": patch
"@effect/ai-openai": patch
"@effect/ai": patch
---

Add Anthropic AI provider integration
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The Effect monorepo is organized into multiple packages, each extending the core
| `effect` | Core package | [README](https://github.com/Effect-TS/effect/blob/main/packages/effect/README.md) |
| `@effect/ai` | AI utilities | [README](https://github.com/Effect-TS/effect/blob/main/packages/ai/ai/README.md) |
| `@effect/ai-openai` | OpenAI utilities | [README](https://github.com/Effect-TS/effect/blob/main/packages/ai/openai/README.md) |
| `@effect/ai-anthropic` | Anthropic utilities | [README](https://github.com/Effect-TS/effect/blob/main/packages/ai/anthropic/README.md) |
| `@effect/cli` | CLI utilities | [README](https://github.com/Effect-TS/effect/blob/main/packages/cli/README.md) |
| `@effect/cluster` | Distributed computing tools | [README](https://github.com/Effect-TS/effect/blob/main/packages/cluster/README.md) |
| `@effect/cluster-browser` | Cluster utilities for the browser | [README](https://github.com/Effect-TS/effect/blob/main/packages/cluster-browser/README.md) |
Expand Down
42 changes: 24 additions & 18 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,28 @@
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
};
outputs = {nixpkgs, ...}: let
forAllSystems = function:
nixpkgs.lib.genAttrs nixpkgs.lib.systems.flakeExposed
(system: function nixpkgs.legacyPackages.${system});
in {
formatter = forAllSystems (pkgs: pkgs.alejandra);
devShells = forAllSystems (pkgs: {
default = pkgs.mkShell {
packages = with pkgs; [
bun
corepack
deno
nodejs
python3
];
};
});
};
outputs =
{ nixpkgs, ... }:
let
forAllSystems =
function:
nixpkgs.lib.genAttrs nixpkgs.lib.systems.flakeExposed (
system: function nixpkgs.legacyPackages.${system}
);
in
{
formatter = forAllSystems (pkgs: pkgs.alejandra);
devShells = forAllSystems (pkgs: {
default = pkgs.mkShell {
packages = with pkgs; [
bun
corepack
deno
nodejs
python3
yq-go
];
};
});
};
}
4 changes: 4 additions & 0 deletions packages/ai/ai/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# `@effect/ai`

## Documentation

- **API Reference**: [View the full documentation](https://effect-ts.github.io/effect/docs/ai/ai).
7 changes: 5 additions & 2 deletions packages/ai/ai/src/AiInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,12 @@ export class ImagePart extends Schema_.TaggedClass<ImagePart>("@effect/ai/AiInpu
)
}

get asBase64(): string {
return Encoding.encodeBase64(this.image.data)
}

get asDataUri(): string {
const base64 = Encoding.encodeBase64(this.image.data)
return `data:${this.image.contentType};base64,${base64}`
return `data:${this.image.contentType};base64,${this.asBase64}`
}
}

Expand Down
24 changes: 23 additions & 1 deletion packages/ai/ai/src/Completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as Effect from "effect/Effect"
import * as HashMap from "effect/HashMap"
import * as JsonSchema from "effect/JSONSchema"
import * as Option from "effect/Option"
import * as Predicate from "effect/Predicate"
import * as Schema from "effect/Schema"
import * as AST from "effect/SchemaAST"
import * as Stream from "effect/Stream"
Expand Down Expand Up @@ -377,7 +378,7 @@ const resolveParts = (
const handler = HashMap.unsafeGet(options.tools.handlers, part.name)
const decodeParams = Schema.decodeUnknown(tool as any)
const encodeSuccess = Schema.encode(tool.success)
return decodeParams(part.params).pipe(
return decodeParams(injectTag(part.params, part.name)).pipe(
Effect.mapError((cause) =>
new AiError({
module: "Completions",
Expand Down Expand Up @@ -410,3 +411,24 @@ const resolveParts = (
Effect.as(new WithResolved({ response: options.response, resolved, encoded }))
)
}

/**
* Certain providers (i.e. Anthropic) do not do a great job returning the
* `_tag` enum with the parameters for a tool call. This method ensures that
* the `_tag` is injected into the tool call parameters to avoid issues when
* decoding.
*/
function injectTag(params: unknown, tag: string) {
// If for some reason we do not receive an object back for the tool call
// input parameters, just return them unchanged
if (!Predicate.isObject(params)) {
return params
}
// If the tool's `_tag` is already present in input parameters, return them
// unchanged
if (Predicate.hasProperty(params, "_tag")) {
return params
}
// Otherwise inject the tool's `_tag` into the input parameters
return { ...params, _tag: tag }
}
21 changes: 21 additions & 0 deletions packages/ai/anthropic/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Effectful Technologies Inc

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
5 changes: 5 additions & 0 deletions packages/ai/anthropic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# `@effect/ai-anthropic`

## Documentation

- **API Reference**: [View the full documentation](https://effect-ts.github.io/effect/docs/ai/ai-anthropic).
25 changes: 25 additions & 0 deletions packages/ai/anthropic/docgen.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"$schema": "../../../node_modules/@effect/docgen/schema.json",
"exclude": ["src/Generated.ts", "src/internal/**/*.ts"],
"examplesCompilerOptions": {
"noEmit": true,
"strict": true,
"skipLibCheck": true,
"moduleResolution": "Bundler",
"module": "ES2022",
"target": "ES2022",
"lib": ["ES2022", "DOM", "DOM.Iterable"],
"paths": {
"effect": ["../../../../effect/src/index.js"],
"effect/*": ["../../../../effect/src/*.js"],
"@effect/experimental": ["../../../../experimental/src/index.js"],
"@effect/experimental/*": ["../../../../experimental/src/*.js"],
"@effect/platform": ["../../../../platform/src/index.js"],
"@effect/platform/*": ["../../../../platform/src/*.js"],
"@effect/ai": ["../../../ai/src/index.js"],
"@effect/ai/*": ["../../../ai/src/*.js"],
"@effect/ai-anthropic": ["../../../ai-anthropic/src/index.js"],
"@effect/ai-anthropic/*": ["../../../ai-anthropic/src/*.js"]
}
}
}
58 changes: 58 additions & 0 deletions packages/ai/anthropic/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"name": "@effect/ai-anthropic",
"type": "module",
"version": "0.0.0",
"license": "MIT",
"description": "Effect modules for working with AI apis",
"homepage": "https://effect.website",
"repository": {
"type": "git",
"url": "https://github.com/Effect-TS/effect.git",
"directory": "packages/ai/anthropic"
},
"bugs": {
"url": "https://github.com/Effect-TS/effect/issues"
},
"tags": [
"typescript",
"algebraic-data-types",
"functional-programming"
],
"keywords": [
"typescript",
"algebraic-data-types",
"functional-programming"
],
"publishConfig": {
"access": "public",
"directory": "dist",
"provenance": true
},
"scripts": {
"codegen": "build-utils prepare-v2",
"build": "pnpm build-esm && pnpm build-annotate && pnpm build-cjs && build-utils pack-v2",
"build-esm": "tsc -b tsconfig.build.json",
"build-cjs": "babel build/esm --plugins @babel/transform-export-namespace-from --plugins @babel/transform-modules-commonjs --out-dir build/cjs --source-maps",
"build-annotate": "babel build/esm --plugins annotate-pure-calls --out-dir build/esm --source-maps",
"check": "tsc -b tsconfig.json",
"test": "vitest",
"coverage": "vitest --coverage"
},
"peerDependencies": {
"@effect/ai": "workspace:^",
"@effect/experimental": "workspace:^",
"@effect/platform": "workspace:^",
"effect": "workspace:^"
},
"devDependencies": {
"@effect/ai": "workspace:^",
"@effect/experimental": "workspace:^",
"@effect/platform": "workspace:^",
"@effect/platform-node": "workspace:^",
"@tim-smart/openapi-gen": "^0.3.6",
"effect": "workspace:^"
},
"dependencies": {
"@anthropic-ai/tokenizer": "^0.0.4"
}
}
10 changes: 10 additions & 0 deletions packages/ai/anthropic/scripts/generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env sh
mkdir -p tmp
anthropic_stats_url="https://raw.githubusercontent.com/anthropics/anthropic-sdk-typescript/refs/heads/main/.stats.yml"
openapi_spec_url=$(curl -sSL $anthropic_stats_url | yq '.openapi_spec_url')
curl $openapi_spec_url > tmp/anthropic.yaml
echo "/**
* @since 1.0.0
*/" > src/Generated.ts
pnpm openapi-gen -s tmp/anthropic.yaml >> src/Generated.ts
pnpm eslint --fix src/Generated.ts
Loading

0 comments on commit 9375c28

Please sign in to comment.