HelgeSverre / mindwave

[WIP] 🧠 Toolkit for building AI features into your Laravel app.
https://mindwave.no
MIT License
67 stars 8 forks source link

Use Proxy To Connect OpenAI #17

Closed icetalker closed 11 months ago

icetalker commented 11 months ago

Looks like there is no way to use http proxy to connect openai in current version, is there any chance to add this feature in the furture?

HelgeSverre commented 11 months ago

The Package uses the Manager pattern, i believe you can overwrite the default "openai" driver by simply doing this:

<?php

namespace App\Providers;

use GuzzleHttp\Client;
use Illuminate\Support\ServiceProvider;
use Mindwave\Mindwave\LLM\Drivers\OpenAI\OpenAI as OpenAIDriver;
use Mindwave\Mindwave\LLM\LLMManager;
use OpenAI;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {

    }

    public function register()
    {
        LLMManager::extend('openai', function () {
            return new OpenAIDriver(
                client: OpenAI::factory()
                    ->withApiKey(config('mindwave-llm.llms.openai.api_key'))
                    ->withOrganization(config('mindwave-llm.llms.openai.org_id'))
                    ->withHttpClient(new Client([
                        'proxy' => 'tcp://12.34.56.78:3128',
                    ]))
                    ->make(),
                model: config('mindwave-llm.llms.openai.model'),
                maxTokens: config('mindwave-llm.llms.openai.max_tokens'),
                temperature: config('mindwave-llm.llms.openai.temperature'),
            );
        });
    }
}
icetalker commented 11 months ago

Thank you for this sample code, this helps me a lot!