Skip to content
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

✨ feat: Support Cloudflare function calling #4671

Open
wants to merge 4 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
4 changes: 2 additions & 2 deletions src/config/modelProviders/cloudflare.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ModelProviderCard } from '@/types/llm';

// ref https://developers.cloudflare.com/workers-ai/models/#text-generation
// api https://developers.cloudflare.com/workers-ai/configuration/open-ai-compatibility
const Cloudflare: ModelProviderCard = {
chatModels: [
{
Expand All @@ -19,7 +18,7 @@ const Cloudflare: ModelProviderCard = {
{
displayName: 'hermes-2-pro-mistral-7b',
enabled: true,
// functionCall: true,
functionCall: true,
id: '@hf/nousresearch/hermes-2-pro-mistral-7b',
tokens: 4096,
},
Expand Down Expand Up @@ -78,6 +77,7 @@ const Cloudflare: ModelProviderCard = {
},
],
checkModel: '@hf/meta-llama/meta-llama-3-8b-instruct',
disableBrowserRequest: false,
id: 'cloudflare',
modelList: {
showModelFetcher: true,
Expand Down
45 changes: 41 additions & 4 deletions src/libs/agent-runtime/cloudflare/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Mock, afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { ChatCompletionTool } from '@/libs/agent-runtime';

import type { OpenAIChatMessage } from '../types';
import * as CloudflareUtils from '../utils/cloudflareHelpers';
import * as debugStreamModule from '../utils/debugStream';
import { LobeCloudflareAI } from './index';

Expand Down Expand Up @@ -272,12 +274,47 @@ describe('LobeCloudflareAI', () => {
});

describe('chat with tools', () => {
it('should call client.beta.tools.messages.create when tools are provided', async () => {
const tools: ChatCompletionTool[] = [
{ function: { name: 'tool1', description: 'desc1' }, type: 'function' },
];

it('should disable stream when tools are provided', async () => {
// Act & Assert
await instance.chat({
messages: [{ content: 'Hello', role: 'user' }],
model: '@hf/meta-llama/meta-llama-3-8b-instruct',
temperature: 1,
tools,
});
expect(globalThis.fetch).toHaveBeenCalled();

const fetchCallArgs = (globalThis.fetch as Mock).mock.calls[0];
const body = JSON.parse(fetchCallArgs[1].body);
expect(body).toEqual(
expect.objectContaining({
stream: false,
}),
);
});

it('should remove plugin info from messages', async () => {
// Arrange
const tools: ChatCompletionTool[] = [
{ function: { name: 'tool1', description: 'desc1' }, type: 'function' },
];
vi.spyOn(CloudflareUtils, 'removePluginInfo').mockImplementation((messages) => messages);
const messages: OpenAIChatMessage[] = [{ content: 'Hello', role: 'user' }];

// Act
await instance.chat({
messages,
model: '@hf/meta-llama/meta-llama-3-8b-instruct',
temperature: 1,
tools,
});

// Assert
expect(CloudflareUtils.removePluginInfo).toHaveBeenCalledWith(messages);
});

it('should include tools', async () => {
// Act
await instance.chat({
messages: [{ content: 'Hello', role: 'user' }],
Expand Down
14 changes: 11 additions & 3 deletions src/libs/agent-runtime/cloudflare/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
convertModelManifest,
desensitizeCloudflareUrl,
fillUrl,
removePluginInfo,
} from '../utils/cloudflareHelpers';
import { AgentRuntimeError } from '../utils/createError';
import { debugStream } from '../utils/debugStream';
Expand Down Expand Up @@ -47,15 +48,22 @@ export class LobeCloudflareAI implements LobeRuntimeAI {

async chat(payload: ChatStreamPayload, options?: ChatCompetitionOptions): Promise<Response> {
try {
const { model, tools, ...restPayload } = payload;
const { messages: _messages, model, stream: _stream, tools, ...restPayload } = payload;
const messages = tools ? removePluginInfo(_messages) : _messages;
const stream = tools ? false : _stream;
const functions = tools?.map((tool) => tool.function);
const headers = options?.headers || {};
if (this.apiKey) {
headers['Authorization'] = `Bearer ${this.apiKey}`;
}
const url = new URL(model, this.baseURL);
const response = await fetch(url, {
body: JSON.stringify({ tools: functions, ...restPayload }),
body: JSON.stringify({
messages,
stream,
tools: functions,
...restPayload,
}),
headers: { 'Content-Type': 'application/json', ...headers },
method: 'POST',
signal: options?.signal,
Expand Down Expand Up @@ -86,7 +94,7 @@ export class LobeCloudflareAI implements LobeRuntimeAI {

return StreamingResponse(
responseBody
.pipeThrough(new TransformStream(new CloudflareStreamTransformer()))
.pipeThrough(new TransformStream(new CloudflareStreamTransformer(stream)))
.pipeThrough(createCallbacksTransformer(options?.callback)),
{ headers: options?.headers },
);
Expand Down
Loading