-
-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support per-request
HttpClient
transformations in the OpenAI AI int…
…egration package (#4418)
- Loading branch information
Showing
6 changed files
with
1,304 additions
and
986 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- | ||
"@effect/ai-openai": patch | ||
--- | ||
|
||
Support per-request HTTP client transformations in the OpenAi AI integration package. | ||
|
||
For example: | ||
|
||
```ts | ||
import { Completions } from "@effect/ai" | ||
import { OpenAiClient, OpenAiCompletions, OpenAiConfig } from "@effect/ai-openai" | ||
import { HttpClient, HttpClientRequest } from "@effect/platform" | ||
import { NodeHttpClient } from "@effect/platform-node" | ||
import { Config, Effect, Layer } from "effect" | ||
|
||
const OpenAi = OpenAiClient.layerConfig({ | ||
apiKey: Config.redacted("OPENAI_API_KEY") | ||
}).pipe(Layer.provide(NodeHttpClient.layerUndici)) | ||
|
||
const Gpt4oCompletions = OpenAiCompletions.layer({ | ||
model: "gpt-4o" | ||
}).pipe(Layer.provide(OpenAi)) | ||
|
||
const program = Effect.gen(function*() { | ||
const completions = yield* Completions.Completions | ||
|
||
yield* completions.create("Tell me a dad joke").pipe( | ||
// Per-request HTTP client transforms which are only applied if | ||
// the OpenAi provider is in use | ||
OpenAiConfig.withClientTransform( | ||
HttpClient.mapRequest( | ||
HttpClientRequest.setHeader("x-dad-jokes", "are-awesome") | ||
) | ||
) | ||
) | ||
}) | ||
|
||
program.pipe(Effect.provide(Gpt4oCompletions), Effect.runPromise) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.