purai.py Methods
Testing
test()
Test the API by recieving a test message. The AI will respond and tell a funny joke if it is online.
Returns
- (dict or str): If in development mode (
dev=True), returns the full JSON response or an error message. Otherwise, returns the AI's response or an error message.
Generate
generate(prompt, provider="openai", beta=False)
Generate individual text based on a prompt.
API Reference
promptRequired (str): The text prompt for the AI.providerOptional (str): The provider for text generation (default is "openai").betaOptional (bool): Whether to use the beta version (default isFalse).
Code sample
import purai
ai = purai.PurAI(token, dev = False)
print(ai.generate("hi!"))Chat
chat(messages, provider="openai", beta=False)
Engage in a chat conversation with AI.
API Reference
messagesRequired (list): List of message objects for the conversation. (Same formatting as with OpenAI's package!)providerOptional (str): The provider for chat completion (default is "openai").betaOptional (bool): Whether to use the beta version (default isFalse).
Code sample
import purai
ai = purai.PurAI(token, dev = False)
messages = { "role": "user", "content": "Hello! How are you?" }
print(ai.chat(messages))Document Analysis
document_analysis(document_url: str, prompt: str, beta=False)
Analyze documents or images with AI.
API Reference
document_urlRequired (str): The URL of the document or image to analyze.promptRequired (str): A prompt for the analysis.betaOptional (bool): Whether to use the beta version (default isFalse).
Code sample
import purai
ai = purai.PurAI(token, dev = False)
print(ai.document_analysis(document_url = "https://online.visual-paradigm.com/repository/images/4367fbb4-b023-4e4f-b910-8db451358a3b/recipe-cards-design/simple-pancake-recipe-card.png", prompt = "What's the first step?"))Contend Moderation
moderations(prompt, beta=False)
Perform content moderation on text and analyze its safety.
API Reference
promptRequired (str): The text to be moderated.betaOptional (bool): Whether to use the beta version (default isFalse).
Code sample
import purai
ai = purai.PurAI(token, dev = False)
print(ai.moderations(prompt = "Hello, there idiot!"))Image Generation
image(prompt, provider="openai", beta=False)
Generate images based on a text prompt.
API Reference
promptRequired (str): The text prompt for image generation.providerOptional (str): The provider for image generation (default is "openai").betaOptional (bool): Whether to use the beta version (default isFalse).
Code sample
import purai
ai = purai.PurAI(token, dev = False)
print(ai.image("A yellow duck walking around the street with a bird as a pet"))Audio Transcription
transcribe(file_url, beta=False)
Transcribe an audio file from its URL.
API Reference
file_urlRequired (str): The URL of the audio file.betaOptional (bool): Whether to use the beta version (default isFalse).
Code sample
import purai
ai = purai.PurAI(token, dev = False)
print(ai.transcribe("https://some.audio/file.mp3"))Audio Translation
translate(file_url, beta=False)
Translate an audio file from its URL.
API Reference
file_urlRequired (str): The URL of the audio file.betaOptional (bool): Whether to use the beta version (default isFalse).
Code sample
import purai
ai = purai.PurAI(token, dev = False)
print(ai.translate("https://file.audio/some.mp3"))Custom
custom(owner, endpoint, body, subdomain)
Use a premade API endpoint within the PurAI project that isn't listed here (usually for beta stuff).
API Reference
ownerRequired (str): The owner/provider of the custom endpoint.endpointRequired (str): The custom endpoint to call.bodyRequired (dict): The request body as a dictionary.subdomainRequired (str): The subdomain to use for the custom request.
Code sample
endpoint and subdomain args in the example below (/v1/characters/someId/chat/completions & rp, respectively) were the indication where, within the ai.purlabs.xyz domain, you would to access the roleplay API (https://rp.ai.purlabs.xyz//v1/characters/someId/chat/completions).import purai
ai = purai.PurAI(token, dev = False)
body = {
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "Hello! How are you?"
}
]
}
print(ai.custom("openai", "/v1/characters/someId/chat/completions", body, "rp"))