{"id":19808795,"url":"https://github.com/premai-io/prem-python-sdk","last_synced_at":"2025-05-01T07:32:48.343Z","repository":{"id":189703934,"uuid":"667484264","full_name":"premAI-io/prem-python-sdk","owner":"premAI-io","description":"The Prem Python SDK is a Python library for interacting with the Prem API","archived":false,"fork":false,"pushed_at":"2025-03-26T08:51:26.000Z","size":3112,"stargazers_count":8,"open_issues_count":2,"forks_count":0,"subscribers_count":11,"default_branch":"main","last_synced_at":"2025-04-06T11:11:31.170Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://premai-io.github.io/prem-python-sdk","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/premAI-io.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-07-17T15:54:01.000Z","updated_at":"2025-03-26T08:51:30.000Z","dependencies_parsed_at":"2024-02-08T15:52:40.805Z","dependency_job_id":"c867d6e7-66e7-4680-8c87-3e52f01a8395","html_url":"https://github.com/premAI-io/prem-python-sdk","commit_stats":null,"previous_names":["premai-io/prem","premai-io/prem-python-sdk"],"tags_count":97,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/premAI-io%2Fprem-python-sdk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/premAI-io%2Fprem-python-sdk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/premAI-io%2Fprem-python-sdk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/premAI-io%2Fprem-python-sdk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/premAI-io","download_url":"https://codeload.github.com/premAI-io/prem-python-sdk/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251840522,"owners_count":21652369,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-12T09:14:48.961Z","updated_at":"2025-05-01T07:32:48.015Z","avatar_url":"https://github.com/premAI-io.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# Prem Python SDK\n\n## Installation\n\nYou can install the Prem Python SDK directly from npm.\n\n```bash\npip install premai\n```\n\n## Usage\n\n### Getting Started\n\nTo use the Prem Python SDK, you need to obtain an API key from the Prem platform. You can then create a `Prem` instance to make requests to the API.\n\n```python\nfrom premai import Prem\n\nclient = Prem(\n    api_key=YOUR_API_KEY\n)\n```\n\n## Chat completion\n\nThe `chat.completions` module allows you to generate completions based on user input.\n\nNote that `system` is NOT an acceptable role: to use a system prompt you should define and pass `system_prompt`.\n\nHere's an example:\n\n```python\nsystem_prompt = \"You're an helpful assistant\"\nmessages = [\n    {\"role\": \"user\", \"content\": \"Who won the world series in 2020?\"},\n]\nproject_id = PROJECT_ID\n\n# Create completion\nresponse = client.chat.completions.create(\n    project_id=project_id,\n    system_prompt=system_prompt,\n    messages=messages,\n)\n\nprint(response.choices)\n```\n\n### Chat completion with stream\n\nYou can also create a completion with a stream to receive the response in chunks by passing the `stream` parameter as `true` (default is `false`).\n\n```python\n# Create completion with stream\nresponse = client.chat.completions.create(\n    project_id=project_id,\n    messages=messages,\n    stream=True,\n)\n\nfor chunk in response:\n    if chunk.choices[0].delta[\"content\"]:\n        print(chunk.choices[0].delta[\"content\"], end=\"\")\n```\n\n### Prompt Templates\n\nShould your operations entail the frequent utilization of identical prompt structures, the **Prompt Template** functionality facilitates the streamlining of this process. It enables the instantiation and subsequent reuse of predefined prompts, thereby optimizing efficiency and maintaining uniformity across interactions.\n\n#### Creating a Prompt Template\nCreate your own Prompt Template in just a few steps:\n\n- Navigate to the **Launchpad** section of your project.\n- Click on the **Templates** tab.\n- Hit the button to create a new Prompt Template.\n\nFrom here, you can either create your custom Prompt Template or choose one of our default presets.\n\nWithin the template, you can include placeholders for dynamic content by using the `${placeholder_name}` syntax, as illustrated below:\n\n```markdown\nSummarize the following text:\n\"\"\"\n${text}\n\"\"\"\n```\n\nIn this example, we have a placeholder named `text`. To implement this through our SDK, follow this sample code:\n\n```python\n# Text you want to summarize\ntext_to_summarize = \"This is the great tale of ... \"\n# Construct the message with the template\nmessages = [\n    {\n        \"role\": \"user\",\n        \"template_id\": TEMPLATE_ID,  # Your template's ID\n        \"params\": {\"text\": text_to_summarize}\n    }\n]\n\nresponse = client.chat.completions.create(\n    project_id=project_id,\n    messages=messages\n)\n```\n\n#### Key Points for Using Prompt Templates\nWhen using prompt templates, remember these important guidelines:\n- Replace the `content` field with `template_id` and `params`.\n- Both `template_id` (the unique ID of your prompt template) and `params` (a key-value object mapping your placeholders to their desired values) are required to utilize prompt templates.\n\nAny keys in `params` not matching placeholders will be ignored. If a placeholder is omitted in `params`, it defaults to an empty string. For instance, if you provide the following message set, the `${text}` placeholder will be left empty:\n\n```python\nmessages = [\n    {\n        \"role\": \"user\",\n        \"template_id\": TEMPLATE_ID,\n        \"params\": {}  # No parameters provided for placeholders\n    }\n]\n```\n### Optional parameters\n\nBy default, the `chat.completions` module uses the default launchpad parameters. You can also specify the following optional parameters:\n\n- `model`: The model to use for completion. If omitted, the default launchpad model will be used.\n- `system_prompt`: The system prompt to use for completion. If omitted, the default launchpad system prompt will be used.\n- `session_id`: A unique identifier to maintain session context, useful for tracking conversations or data across multiple requests.\n- `temperature`: The temperature to use for completion. If omitted, the default launchpad temperature will be used.\n- `max_tokens`: The maximum number of tokens to generate for completion. If omitted, the default launchpad max tokens will be used.\n\nExample:\n\n```python\nmodel = \"gpt-3.5-turbo\"\nsystem_prompt = \"You are a helpful assistant.\"\nsession_id = \"my-session\"\ntemperature = 0.7\nmessages = [\n    { \"role\": \"user\", \"content\": \"Who won the world series in 2020?\" },\n]\n\nresponse = client.chat.completions.create(\n    project_id=project_id,\n    messages=messages,\n    model=model,\n    system_prompt=system_prompt,\n    session_id=session_id,\n    temperature=temperature\n)\n\nprint(response)\n```\n\n## Enhanced Chat Completion with Retrieval Augmented Generation (RAG)\n\nEnhance your chat completions by leveraging contextual data from specified `repositories`. A `repository` is a collection of `documents`, each containing information that can be utilized by the RAG system to provide enriched and context-aware responses.\n\n**If you've linked your repositories in the launchpad, relax—you're all set for effortless chat completions!** The system automatically uses those parameters by default, ensuring a seamless and easy experience. However, if you wish to customize the process, you can specify the `repositories` parameter to fit your exact needs. Just define:\n\n-   `ids`: Your selected repository IDs.\n-   `similarity_threshold`: The least similarity score for content relevance.\n-   `limit`: The number of content pieces to include.\n\nFor guidance on managing repositories, see the [Repositories](#repositories) section.\n\n```python\nmessages = [\n    { \"role\": \"user\", \"content\": \"Which is Jack's pet name?\" },\n]\n\nrepositories = dict(\n  ids=[REPOSITORY_ID, ...],\n  similarity_threshold=0.65,\n  limit=3\n)\n\n# Create completion\nresponse = client.chat.completions.create(\n  project_id=PROJECT_ID,\n  messages=messages,\n  repositories=repositories,\n  stream=False\n)\n\nprint(response.choices[0].message.content)\n# \"Jack's pet name is Sparky.\"\n\nprint(response.document_chunks)\n# E.g., [DocumentChunks(repository_id=4, document_id=14, chunk_id=15, document_name=\"pets_and_their_owners.txt\", similarity_score=0.67, content=\"...\"), ...]\n```\n\n## Repositories\nRepositories act as storage for documents, organized to facilitate efficient information retrieval. Manipulating repository content is straightforward.\n### Repository  creation\nTo create a repository, you can use the `create` method provided by the `repositories` API. Here's an example of how to create a repository:\n```python\n\nresponse = client.repositories.create(\n\tname=\"Test\",\n\tdescription=\"Test Repository\",\n    organization=\"org-test@premai.io\"\n)\n```\n### Document creation\nTo add a document to a repository, you can use the `create` method provided by the `document` API. Here's an example of how to create and upload a document:\n\n```python\nFILE_PATH = \"pets_and_their_owners.txt\"\n# Content: \"My friend Jack has a beautiful pet, he gave it the name Sparky, [...]\"\n\nresponse = client.repository.document.create(\n\trepository_id=REPOSITORY_ID,\n\tfile=FILE_PATH\n)\n\nprint(response)\n# E.g., DocumentOutput(repository_id=4, document_id=14, name=\"pets_and_their_owners.txt\", type=\"text\", status=\"UPLOADED\", chunk_count=0, error=None)\n```\n\nAfter uploading, the document state is reflected in fields such as:\n\n-   `status`: Shows `UPLOADED` initially, changes once processed (e.g., `PROCESSING`).\n-   `chunk_count`: Number of data chunks; starts at 0 and increases post-processing.\n-   `error`: Non-null if an error arose during processing.\n\nWe currently support below file formats for document uploads:\n-   `.txt`\n-   `.pdf`\n-   `.docx`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpremai-io%2Fprem-python-sdk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpremai-io%2Fprem-python-sdk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpremai-io%2Fprem-python-sdk/lists"}