Skip to content

Commit ce6bd7e

Browse files
authored
fix: add apiKey to model (#60)
1 parent 0d1d6a4 commit ce6bd7e

File tree

4 files changed

+23
-16
lines changed

4 files changed

+23
-16
lines changed

src/agents/adapters.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,21 @@ import { tool } from "ai";
99
import { AISDKTool, LangchainTool } from "./types";
1010
import { AGENT_NAME_HEADER } from "./constants";
1111

12-
type ModelParams = Parameters<ReturnType<typeof createWorkflowOpenAI>>;
13-
type ModelSettingsWithBaseURL = ModelParams["1"] & { baseURL?: string };
14-
export type ModelParamsWithBaseURL = [ModelParams[0], ModelSettingsWithBaseURL?];
15-
1612
/**
1713
* creates an AI SDK openai client with a custom
1814
* fetch implementation which uses context.call.
1915
*
2016
* @param context workflow context
2117
* @returns ai sdk openai
2218
*/
23-
export const createWorkflowOpenAI = (context: WorkflowContext, baseURL?: string) => {
19+
export const createWorkflowOpenAI = (
20+
context: WorkflowContext,
21+
config?: { baseURL?: string; apiKey?: string }
22+
) => {
23+
const { baseURL, apiKey } = config ?? {};
2424
return createOpenAI({
2525
baseURL,
26+
apiKey,
2627
compatibility: "strict",
2728
fetch: async (input, init) => {
2829
try {

src/agents/index.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
import { WorkflowContext } from "../context";
2-
import { createWorkflowOpenAI, ModelParamsWithBaseURL, wrapTools } from "./adapters";
2+
import { createWorkflowOpenAI, wrapTools } from "./adapters";
33
import { Agent } from "./agent";
44
import { Task } from "./task";
55
import {
66
AgentParameters,
77
AISDKTool,
8+
CustomModelParams,
89
LangchainTool,
910
MultiAgentTaskParams,
1011
SingleAgentTaskParams,
1112
} from "./types";
1213

13-
export { createWorkflowOpenAI } from "./adapters";
14-
export { Agent } from "./agent";
15-
1614
/**
1715
* Workflow Agents API
1816
*
@@ -94,10 +92,10 @@ export class WorkflowAgents {
9492
/**
9593
* creates an openai model for agents
9694
*/
97-
public openai(...params: ModelParamsWithBaseURL) {
95+
public openai(...params: CustomModelParams) {
9896
const [model, settings] = params;
99-
const { baseURL, ...otherSettings } = settings ?? {};
100-
const openai = createWorkflowOpenAI(this.context, baseURL);
97+
const { baseURL, apiKey, ...otherSettings } = settings ?? {};
98+
const openai = createWorkflowOpenAI(this.context, { baseURL, apiKey });
10199
return openai(model, otherSettings);
102100
}
103101
}

src/agents/task.test.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import { z } from "zod";
99
import { DisabledWorkflowContext } from "../serve/authorization";
1010

1111
export const getAgentsApi = ({ disabledContext }: { disabledContext: boolean }) => {
12-
const token = getWorkflowRunId();
13-
const workflowRunId = nanoid();
12+
const workflowRunId = getWorkflowRunId();
13+
const token = nanoid();
1414

1515
let context: WorkflowContext;
1616
if (disabledContext) {
@@ -130,9 +130,12 @@ describe("tasks", () => {
130130

131131
test("multi agent with baseURL", async () => {
132132
const { agentsApi, agent, token, workflowRunId } = getAgentsApi({ disabledContext: false });
133+
134+
const customURL = "https://api.deepseek.com/v1";
135+
const customApiKey = nanoid();
133136
const task = agentsApi.task({
134137
agents: [agent],
135-
model: agentsApi.openai("gpt-3.5-turbo", { baseURL: "https://api.deepseek.com/v1" }),
138+
model: agentsApi.openai("gpt-3.5-turbo", { baseURL: customURL, apiKey: customApiKey }),
136139
maxSteps: 2,
137140
prompt: "hello world!",
138141
});
@@ -173,7 +176,7 @@ describe("tasks", () => {
173176
"upstash-callback-workflow-url": "https://requestcatcher.com/api",
174177
"upstash-failure-callback-retries": "3",
175178
"upstash-feature-set": "WF_NoDelete,InitialBody",
176-
"upstash-forward-authorization": `Bearer ${openaiToken}`,
179+
"upstash-forward-authorization": `Bearer ${customApiKey}`,
177180
"upstash-forward-content-type": "application/json",
178181
"upstash-forward-upstash-agent-name": "Manager LLM",
179182
"upstash-method": "POST",

src/agents/types.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { CoreTool, generateText } from "ai";
22
import { Agent } from "./agent";
3+
import { createWorkflowOpenAI } from "./adapters";
34

45
export type AISDKTool = CoreTool;
56
export type LangchainTool = {
@@ -88,3 +89,7 @@ export type ManagerAgentParameters = {
8889
model: Model;
8990
} & Pick<Partial<AgentParameters>, "name" | "background"> &
9091
Pick<AgentParameters, "maxSteps">;
92+
93+
type ModelParams = Parameters<ReturnType<typeof createWorkflowOpenAI>>;
94+
type CustomModelSettings = ModelParams["1"] & { baseURL?: string; apiKey?: string };
95+
export type CustomModelParams = [ModelParams[0], CustomModelSettings?];

0 commit comments

Comments
 (0)