Whitepaper
Docs
Sign In
Function
Function
pipe
v0.2.1
Perplexity
Function ID
perplexity
Creator
@pablocerdeira
Downloads
75+
Perplexity API Manifold
Get
README
No README available
Function Code
Show
""" title: Perplexity Manifold Pipe author: justinh-rahb and moblangeois update: pablo cerdeira author_url: https://github.com/open-webui funding_url: https://github.com/open-webui version: 0.2.1 license: MIT notes: update to Sonar Reasoning Pro, Sonar Reasoning, Sonar Pro, Sonar and r1-1776 """ from pydantic import BaseModel, Field from typing import Optional, Union, Generator, Iterator from open_webui.utils.misc import get_last_user_message from open_webui.utils.misc import pop_system_message import os import requests class Pipe: class Valves(BaseModel): NAME_PREFIX: str = Field( default="Perplexity/", description="The prefix applied before the model names.", ) PERPLEXITY_API_BASE_URL: str = Field( default="https://api.perplexity.ai", description="The base URL for Perplexity API endpoints.", ) PERPLEXITY_API_KEY: str = Field( default="", description="Required API key to access Perplexity services.", ) def __init__(self): self.type = "manifold" self.valves = self.Valves() def pipes(self): return [ { "id": "sonar-reasoning-pro", "name": f"{self.valves.NAME_PREFIX}Sonar Reasoning Pro", }, { "id": "sonar-reasoning", "name": f"{self.valves.NAME_PREFIX}Sonar Reasoning", }, { "id": "sonar-pro", "name": f"{self.valves.NAME_PREFIX}Sonar Pro", }, { "id": "sonar", "name": f"{self.valves.NAME_PREFIX}Sonar", }, { "id": "r1-1776", "name": f"{self.valves.NAME_PREFIX}r1-1776", }, ] def pipe(self, body: dict, __user__: dict) -> Union[str, Generator, Iterator]: print(f"pipe:{__name__}") if not self.valves.PERPLEXITY_API_KEY: raise Exception("PERPLEXITY_API_KEY not provided in the valves.") headers = { "Authorization": f"Bearer {self.valves.PERPLEXITY_API_KEY}", "Content-Type": "application/json", "accept": "application/json", } system_message, messages = pop_system_message(body.get("messages", [])) system_prompt = "You are a helpful assistant." if system_message is not None: system_prompt = system_message["content"] model_id = body["model"] if model_id.startswith(self.valves.NAME_PREFIX): model_id = model_id[len(self.valves.NAME_PREFIX) :] if model_id.startswith("perplexity."): model_id = model_id[len("perplexity.") :] payload = { "model": model_id, "messages": [{"role": "system", "content": system_prompt}, *messages], "stream": body.get("stream", True), "return_citations": True, "return_images": True, } print(payload) try: r = requests.post( url=f"{self.valves.PERPLEXITY_API_BASE_URL}/chat/completions", json=payload, headers=headers, stream=True, ) r.raise_for_status() if body.get("stream", False): return r.iter_lines() else: response = r.json() formatted_response = { "id": response["id"], "model": response["model"], "created": response["created"], "usage": response["usage"], "object": response["object"], "choices": [ { "index": choice["index"], "finish_reason": choice["finish_reason"], "message": { "role": choice["message"]["role"], "content": choice["message"]["content"], }, "delta": {"role": "assistant", "content": ""}, } for choice in response["choices"] ], } return formatted_response except Exception as e: return f"Error: {e}"