Skip to content

Gradio plugin integration to covalent #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const zee = new ZeeWorkflow({
})();
```

### Refer Configs for other SDK's Integrations like Hugging face, Gemini, Gradio etc. on [llms.mdx Readme File](https://github.com/covalenthq/ai-agent-sdk/blob/main/docs/concepts/llms.mdx)
## 🤝 Contributing

Contributions, issues and feature requests are welcome!
Expand Down
71 changes: 71 additions & 0 deletions docs/concepts/llms.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ const llm = new LLM({
"gemini-1.5-pro"
```

```plaintext Gradio
"Any Supported and hosted inference and generation algorithms using gradio python library"
```

</CodeGroup>

## Environment Variables
Expand All @@ -69,6 +73,73 @@ GEMINI_API_KEY

</CodeGroup>

## Example Usage:

For deepseek-ai/deepseek-vl2-small:

```typescript
const gradioConfig: GradioConfig = {
provider: "GRADIO",
name: "deepseek-vl2-small",
appUrl: "deepseek-ai/deepseek-vl2-small",
parameters: [
[["Hello!", null]],
0.9, // topP
0.1, // temperature
0, // repetitionPenalty
100, // maxGenerationTokens
0, // maxHistoryTokens
"deepseek-ai/deepseek-vl2-small", // model name
],
};

const llm = new LLM(gradioConfig);
const response = await llm.generate(
[{ role: "user", content: "Hello!" }],
{ text: z.string() },
{}
);
console.log(response.value);
```
For r3gm/RVC_HF:
```typescript
const gradioConfig: GradioConfig = {
provider: "GRADIO",
name: "RVC_HF",
appUrl: "r3gm/RVC_HF",
endpoint: "/run_infer_script",
parameters: {
f0up_key: 0,
filter_radius: 5,
index_rate: 0,
rms_mix_rate: 0,
protect: 0,
hop_length: 1,
f0method: "rmvpe",
input_path: "https://raw.githubusercontent.com/Philotheephilix/Tunify/main/public/amidreaming.wav",
output_path: "/home/user/app/assets/audios/output.wav",
pth_path: "logs/Justin_Bieber_v2/Justin_Bieber_v2.pth",
index_path: "logs/Justin_Bieber_v2/added_IVF1005_Flat_nprobe_1_Justin_Bieber_v2_v2.index",
clean_strength: 0,
export_format: "WAV",
embedder_model: "hubert",
embedder_model_custom: "",
upscale_audio: true,
},
};
```
Gradio is used for custom made inference and algorithms so the working depends solely on the developer of the Gradio Project and the api configured for it.

### Initialization example with Gradio

const llm = new LLM(gradioConfig);
const response = await llm.generate(
[], // Messages not used for this model
{ result: z.string() },
{}
);
console.log(response.value);

## Use Cases

### Image Analysis
Expand Down
1 change: 1 addition & 0 deletions packages/ai-agent-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
},
"dependencies": {
"@covalenthq/client-sdk": "^2.2.3",
"@gradio/client": "^1.11.0",
"commander": "^13.1.0",
"dotenv": "^16.4.7",
"openai": "^4.79.1",
Expand Down
15 changes: 14 additions & 1 deletion packages/ai-agent-sdk/src/core/llm/llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
import type { AnyZodObject } from "zod";
import { z } from "zod";
import { zodToJsonSchema } from "zod-to-json-schema";
import { Client as GradioClient } from "@gradio/client";

const entryToObject = ([key, value]: [string, AnyZodObject]) => {
return z.object({ type: z.literal(key), value });
Expand Down Expand Up @@ -60,10 +61,11 @@ export class LLM extends Base {
response_schema: T,
tools: Record<string, Tool>
): Promise<FunctionToolCall | LLMResponse<T>> {
const provider = this.model.provider;

const config: ConstructorParameters<typeof OpenAI>[0] = {
apiKey: this.model.apiKey,
};
const provider = this.model.provider;
switch (provider) {
case "OPEN_AI":
break;
Expand All @@ -83,6 +85,8 @@ export class LLM extends Base {
config.apiKey =
process.env["GEMINI_API_KEY"] || this.model.apiKey;
break;
case "GRADIO":
break;
default:
var _exhaustiveCheck: never = provider;
throw new Error(
Expand Down Expand Up @@ -157,6 +161,15 @@ export class LLM extends Base {
} as LLMResponse<T>;
}

if (provider === "GRADIO") {
const config = this.model as any;
const client = await new GradioClient(config.appUrl);
const result = await client.predict(config.endpoint || "/predict", config.parameters || []);
const [schemaKey, schemaValue] = Object.entries(response_schema)[0] || [];
if (!schemaKey || !schemaValue) throw new Error("Invalid schema");
return { type: schemaKey, value: schemaValue.parse(result.data) } as LLMResponse<T>;
}

if (parsed?.response) {
return parsed.response as LLMResponse<T>;
}
Expand Down
27 changes: 26 additions & 1 deletion packages/ai-agent-sdk/src/core/llm/llm.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,22 @@ export type GeminiConfig = {
apiKey?: string;
};

export type GradioConfig = {
provider: "GRADIO";
name: string;
appUrl: string;
endpoint?: string;
parameters?: unknown[];
temperature?: number;
apiKey?: string;
};

export type ModelConfig =
| OpenAIConfig
| DeepSeekConfig
| GrokConfig
| GeminiConfig;
| GeminiConfig
| GradioConfig;

export type LLMResponse<T extends Record<string, AnyZodObject>> = {
[K in keyof T]: {
Expand All @@ -64,3 +75,17 @@ export type FunctionToolCall = {
type: "tool_call";
value: ParsedFunctionToolCall[];
};

export type ChatCompletionMessageParam = {
role: "user" | "assistant" | "system";
content: string;
};

export type ChatCompletionTool = {
type: "function";
function: {
name: string;
description?: string;
parameters: Record<string, unknown>;
};
};