Skip to content

Commit

Permalink
Merge branch 'main' into chromadb-python-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
mehrinkiani committed Feb 7, 2024
2 parents 928395e + 4d2fe06 commit 83477d9
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 19 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/javascript-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@ on:
types:
- opened
- labeled
- synchronize


jobs:
test:
runs-on: ubuntu-latest
if: contains(github.event.pull_request.labels.*.name, 'okay-to-test') || (github.event_name == 'push')
# We'll run if one of the following is met:
# 1. The 'okay-to-test' labeled has just been added to the PR.
# 2. A member or collaborator opens a PR or pushes new commits to it.
# 3. This is a push to the main branch.
if: |
(contains(github.event.pull_request.labels.*.name, 'okay-to-test') && github.event.action == 'labeled') ||
contains(fromJson('["MEMBER", "COLLABORATOR"]'), github.event.pull_request.author_association) ||
github.event_name == 'push'
steps:
# https://github.com/actions/checkout/issues/518
Expand Down
10 changes: 9 additions & 1 deletion .github/workflows/python-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@ on:
types:
- opened
- labeled
- synchronize

jobs:
test:
runs-on: ubuntu-latest
if: contains(github.event.pull_request.labels.*.name, 'okay-to-test') || (github.event_name == 'push')
# We'll run if one of the following is met:
# 1. The 'okay-to-test' labeled has just been added to the PR.
# 2. A member or collaborator opens a PR or pushes new commits to it.
# 3. This is a push to the main branch.
if: |
(contains(github.event.pull_request.labels.*.name, 'okay-to-test') && github.event.action == 'labeled') ||
contains(fromJson('["MEMBER", "COLLABORATOR"]'), github.event.pull_request.author_association) ||
github.event_name == 'push'
steps:
# https://github.com/actions/checkout/issues/518
Expand Down
32 changes: 19 additions & 13 deletions javascript-sdk/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import crypto from "crypto";
import { SdkConfig } from "./config";
import initVectorStore from "./lib/vectordb";
import getOpenAIInstance from "./lib/openai";
import { OpenAIApi } from "openai";
import { VectorStore } from "langchain/vectorstores/base";
import { Document } from "langchain/document";
import Strategy from "./lib/Strategy";
Expand All @@ -28,38 +27,45 @@ export default class RebuffSdk implements Rebuff {
private strategies: Record<string, Strategy> | undefined;
private defaultStrategy: string;

private openai: {
conn: OpenAIApi;
model: string;
};

/**
* @deprecated Use `RebuffSdk.init` instead.
*/
constructor(config: SdkConfig) {
// We're keeping this constructor for backwards compatibility. In the future, we can make it private and
// simplify this class quite a bit.

this.sdkConfig = config;
this.openai = {
conn: getOpenAIInstance(config.openai.apikey),
model: config.openai.model || "gpt-3.5-turbo",
};
this.defaultStrategy = "standard";
}

public static async init(config: SdkConfig): Promise<RebuffSdk> {
const sdk = new RebuffSdk(config);
sdk.vectorStore = await initVectorStore(config);
sdk.strategies = await sdk.getStrategies();
return sdk;
}

private async getStrategies(): Promise<Record<string, Strategy>> {
if (this.strategies) {
return this.strategies;
}
const openai = {
conn: getOpenAIInstance(this.sdkConfig.openai.apikey),
model: this.sdkConfig.openai.model || "gpt-3.5-turbo",
};
const heuristicScoreThreshold = 0.75;
const vectorScoreThreshold = 0.9;
const openaiScoreThreshold = 0.9;
const strategies: Record<string, Strategy> = {
this.strategies = {
// For now, this is the only strategy.
"standard": {
tactics: [
new Heuristic(heuristicScoreThreshold),
new Vector(vectorScoreThreshold, await this.getVectorStore()),
new OpenAI(openaiScoreThreshold, this.openai.model, this.openai.conn),
new OpenAI(openaiScoreThreshold, openai.model, openai.conn),
]
},
};
this.strategies = strategies;
return this.strategies;
}

Expand Down
6 changes: 3 additions & 3 deletions javascript-sdk/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* eslint-disable @typescript-eslint/no-unused-expressions */
import { describe } from "mocha";
import { expect } from "chai";
import { DetectRequest, DetectResponse, TacticName, TacticResult } from "../src/interface";
import { DetectRequest, DetectResponse, TacticName } from "../src/interface";
import RebuffSDK from "../src/sdk";
import { getEnvironmentVariable } from "./helpers";

// Initialize the Rebuff SDK with a real API token and URL
const rb = new RebuffSDK({
const rb = await RebuffSDK.init({
openai: {
apikey: getEnvironmentVariable("OPENAI_API_KEY"),
model: "gpt-3.5-turbo",
Expand All @@ -19,7 +19,7 @@ const rb = new RebuffSDK({
}
}
});
const rb_chroma = new RebuffSDK({
const rb_chroma = await RebuffSDK.init({
openai: {
apikey: getEnvironmentVariable("OPENAI_API_KEY"),
model: "gpt-3.5-turbo",
Expand Down
2 changes: 1 addition & 1 deletion javascript-sdk/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es2015",
"target": "es2017",
"lib": ["dom", "dom.iterable", "esnext"],
"module": "esnext",
"strict": true,
Expand Down

0 comments on commit 83477d9

Please sign in to comment.