ItzCrazyKns / Perplexica

Perplexica is an AI-powered search engine. It is an Open source alternative to Perplexity AI
MIT License
13.67k stars 1.3k forks source link

Custom API key for Ollama Endpoints #227

Closed ProjectMoon closed 3 months ago

ProjectMoon commented 3 months ago

Is your feature request related to a problem? Please describe. While ollama does not support authentication yet directly, many things that expose an ollama endpoint support authentication with the ollama API, and it's recommended to proxy it with an authentication header if exposed publicly. For example, Open WebUI proxies the ollama endpoint and requires a user's API key to use it.

Describe the solution you'd like Right now, the code is not setting an Authorization header for ollama. A new setting for the authorization header can be added. It would be best for this to just be arbitrary text, instead of an API key that has Bearer prepended to it. So the config option would be like Ollama Authorization Header = Bearer .... The reasoning for this is because some people might expose ollama with different authorization header values, instead of Bearer + API key.

Describe alternatives you've considered Some sort of odd configuration where perplexica can bypass the ollama authentication, but this is complicated and requires funny rewrite rules in a reverse proxy and isn't really scalable.

Additional context The biggest value add of this feature is using proxied ollama endpoints like Open Web UI, which can aggregate multiple ollama servers together into one transparent endpoint. In my case, I have the ollama embedding model running on a different machine than the main text generation models, in order to save on model load times.

ItzCrazyKns commented 3 months ago

Is your feature request related to a problem? Please describe. While ollama does not support authentication yet directly, many things that expose an ollama endpoint support authentication with the ollama API, and it's recommended to proxy it with an authentication header if exposed publicly. For example, Open WebUI proxies the ollama endpoint and requires a user's API key to use it.

Describe the solution you'd like Right now, the code is not setting an Authorization header for ollama. A new setting for the authorization header can be added. It would be best for this to just be arbitrary text, instead of an API key that has Bearer prepended to it. So the config option would be like Ollama Authorization Header = Bearer .... The reasoning for this is because some people might expose ollama with different authorization header values, instead of Bearer + API key.

Describe alternatives you've considered Some sort of odd configuration where perplexica can bypass the ollama authentication, but this is complicated and requires funny rewrite rules in a reverse proxy and isn't really scalable.

Additional context The biggest value add of this feature is using proxied ollama endpoints like Open Web UI, which can aggregate multiple ollama servers together into one transparent endpoint. In my case, I have the ollama embedding model running on a different machine than the main text generation models, in order to save on model load times.

Hi man, right now, you can make use of the custom OpenAI provider and add the Ollama URL in their along with the API key. As of now we cannot add the option for custom Ollama headers as it might be confusing to most of the people (the current implementation is confusing as well). If someone has implemented some other type of authentication system I believe they have the knowledge to simply change a simple keyword in the providers file and get it to work. But we can think of this in the future when we're done with most of the things.

ProjectMoon commented 3 months ago

Hmm, when setting the custom OpenAI base URL, I cannot enter the /v1 or https in the settings. Is that intended behavior? :thinking:

ProjectMoon commented 3 months ago

OK I hacked this in as a new toml setting on my own fork. If you want, I can make a PR for it. But otherwise, I will just maintain it on my own version. I just added a new setting to the TOML, and then pass it as an HTTP header if present.

bpawnzZ commented 2 months ago

OK I hacked this in as a new toml setting on my own fork. If you want, I can make a PR for it. But otherwise, I will just maintain it on my own version. I just added a new setting to the TOML, and then pass it as an HTTP header if present.

Can you please post your code example. Thank you!

ProjectMoon commented 2 months ago

OK I hacked this in as a new toml setting on my own fork. If you want, I can make a PR for it. But otherwise, I will just maintain it on my own version. I just added a new setting to the TOML, and then pass it as an HTTP header if present.

Can you please post your code example. Thank you!

https://git.agnos.is/projectmoon/perplexica/compare/c62684407de848b3d4c01e29be7e57d3bfc4da2d...452cb0e91f98db92b8bc88d23fece895cf490b1a

Needs some cleanup, but this is what I did. Some leftover unused variables and stuff. Also no UI setting.