Skip to content

Commit

Permalink
fix: HELICONE环境变量问题
Browse files Browse the repository at this point in the history
  • Loading branch information
MarleneJiang committed Sep 29, 2023
1 parent 64c1aad commit 4d80732
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
3 changes: 1 addition & 2 deletions package/llm/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { LLM, LLM_OPS_CONFIG, createMessage } from "llm-ops";
import { PipelineContext } from "@idealeap/pipeline";
test("测试LLM的对话 & 替换log函数", async () => {
LLM.log = (...args: string[]) => {
console.log("test:", args);
Expand Down Expand Up @@ -29,7 +28,7 @@ test("测试LLM的对话 & 替换log函数", async () => {
});

test("全局设置Config", async () => {
LLM_OPS_CONFIG.OPENAI_API_KEY = "";
// LLM_OPS_CONFIG.OPENAI_API_KEY = "";
class testA {
async a() {
const llm = new LLM({});
Expand Down
49 changes: 31 additions & 18 deletions package/llm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,26 @@ export class LLM {
const openAIApiKey = OPENAI_API_KEY || LLM_OPS_CONFIG.OPENAI_API_KEY;
if (!openAIApiKey) {
this.missingEnvironmentVariable(
"OPENAI_API_KEY Missing! 😅 It's not free!",
"OPENAI_API_KEY Missing! 😅 It's not free!"
);
}
const config =
LLM_OPS_CONFIG.HELICONE_AUTH_API_KEY || HELICONE_AUTH_API_KEY
? LLM_OPS_CONFIG.OPEN_PATH
: {};
const config = !!HELICONE_AUTH_API_KEY
? {
baseURL: "https://oai.hconeai.com/v1",
defaultHeaders: {
"Helicone-Auth": `Bearer ${process.env["HELICONE_AUTH_API_KEY"]}`,
},
}
: !!LLM_OPS_CONFIG.OPEN_PATH
? LLM_OPS_CONFIG.OPEN_PATH
: LLM_OPS_CONFIG.HELICONE_AUTH_API_KEY
? {
baseURL: "https://oai.hconeai.com/v1",
defaultHeaders: {
"Helicone-Auth": `Bearer ${LLM_OPS_CONFIG.HELICONE_AUTH_API_KEY}`,
},
}
: {};
return new OpenAI({
...config,
apiKey: openAIApiKey,
Expand Down Expand Up @@ -209,7 +222,7 @@ export class LLM {
user: this.user,
};
const res = (await this.llm.chat.completions.create(
params_,
params_
)) as chatCompletionType;
this.tokens += res.usage?.total_tokens || 0;
!!this.cache &&
Expand Down Expand Up @@ -270,7 +283,7 @@ export class LLM {
user: this.user,
};
const res = (await this.llm.chat.completions.create(
params_,
params_
)) as chatCompletionType;
this.tokens += res.usage?.total_tokens || 0;
!!this.cache &&
Expand All @@ -291,7 +304,7 @@ export class LLM {
}

async embedding(
input: string | string[] | number[] | number[][],
input: string | string[] | number[] | number[][]
): Promise<resEmbeddingType> {
return await this.llm.embeddings.create({
input: input,
Expand All @@ -310,29 +323,29 @@ export class LLM {
`%c system ${message.name ? "(" + message.name + ")" : ""}: ${
message.content
} \n`,
`color: ${this.roleToColor[message.role]}`,
`color: ${this.roleToColor[message.role]}`
);
} else if (message.role === "user") {
LLM.log(
`%c user: ${message.content} \n`,
`color: ${this.roleToColor[message.role]}`,
`color: ${this.roleToColor[message.role]}`
);
} else if (message.role === "assistant" && message.function_call) {
LLM.log(
`%c assistant: ${JSON.stringify(message.function_call)} \n`,
`color: ${this.roleToColor[message.role]}`,
`color: ${this.roleToColor[message.role]}`
);
} else if (message.role === "assistant" && !message.function_call) {
LLM.log(
`%c assistant: ${message.content} \n`,
`color: ${this.roleToColor[message.role]}`,
`color: ${this.roleToColor[message.role]}`
);
} else if (message.role === "function") {
LLM.log(
`%c function (${
message.name // response message has not `name`
}): ${message.content} \n`,
`color: ${this.roleToColor[message.role]}`,
`color: ${this.roleToColor[message.role]}`
);
}
}
Expand All @@ -343,27 +356,27 @@ export class LLM {
if (message.role === "system") {
LLM.log(
`%c system: ${message.content} \n`,
`color: ${this.roleToColor[message.role]}`,
`color: ${this.roleToColor[message.role]}`
);
} else if (message.role === "user") {
LLM.log(
`%c user: ${message.content} \n`,
`color: ${this.roleToColor[message.role]}`,
`color: ${this.roleToColor[message.role]}`
);
} else if (message.role === "assistant" && message.function_call) {
LLM.log(
`%c assistant: ${JSON.stringify(message.function_call)} \n`,
`color: ${this.roleToColor[message.role]}`,
`color: ${this.roleToColor[message.role]}`
);
} else if (message.role === "assistant" && !message.function_call) {
LLM.log(
`%c assistant: ${message.content} \n`,
`color: ${this.roleToColor[message.role]}`,
`color: ${this.roleToColor[message.role]}`
);
} else if (message.role === "function") {
LLM.log(
`%c function : ${message.content} \n`,
`color: ${this.roleToColor[message.role]}`,
`color: ${this.roleToColor[message.role]}`
);
}
}
Expand Down
File renamed without changes.

0 comments on commit 4d80732

Please sign in to comment.