Api client for Grazie services
Project description
Grazie Api Gateway Client
Note, this package is deprecated, please refer to Grazie Api Gateway Client V2 first and check if the new client library supports functionality you need.
This package provides api client for JetBrains AI Platform llm functionality. Supported methods are chat, completion and embeddings.
Support for Grazie NLP services is planned in the future.
You can try models in the browser by going to https://try.ai.intellij.net/ or using the command-line interface.
poetry run -C libs/grazie_api_gateway_client python3 -m grazie.api.client -p openai-gpt-4 chat 'Who was the most famous pop star in the 90s?'
Usage
First you have to create an instance of client, please check class documentation to know more about parameters:
client = GrazieApiGatewayClient(
grazie_agent=GrazieAgent(name="grazie-api-gateway-client-readme", version="dev"),
url=GrazieApiGatewayUrls.STAGING,
auth_type=AuthType.USER,
grazie_jwt_token=***
)
Below are examples of usage by method:
Completion
client.complete(
prompt=CompletionPrompt("Once upon a time there was a unicorn. "),
profile=Profile.GRAZIE_GPT_NEO_TINY_TEXT,
)
Chat
client.chat(
chat=ChatPrompt()
.add_system("You are a helpful assistant.")
.add_user("Who won the world series in 2020?"),
profile=Profile.OPENAI_CHAT_GPT
)
Additionally you can pass id of your prompt or feature via prompt_id parameter.
This identifier can later be used to check spending and calculate price of the feature per user or
per call.
If you develop prompt which should answer in a structured format (i.e. JSON) it's better to pass temperature = 0. This makes generation deterministic (almost) and will provide parsable responses more reliably.
client.chat(
chat=ChatPrompt()
.add_system("You are a helpful assistant.")
.add_user("Who won the world series in 2020?"),
profile=Profile.OPENAI_CHAT_GPT,
parameters={
LLMParameters.Temperature: Parameters.FloatValue(0.0)
}
)
Note: this parameter is currently only supported for OpenAI models.
Streaming
Outputs from chat models can be slow, to show progress to a user you can call chat_stream. The output would be a stream of text chunks.
response = ""
for chunk in client.chat_stream(
chat=ChatPrompt()
.add_user("Who won the world series in 2020?")
.add_assistant("The Los Angeles Dodgers won the World Series in 2020.")
.add_user("Where was it played? Write a small poem about it!"),
profile=Profile.OPENAI_CHAT_GPT
):
response += chunk.chunk
Embeddings
You can also use api to build float vector embeddings for sentences and texts.
client.embed(
request=EmbeddingRequest(texts=["Sky is blue."], model="sentence-transformers/LaBSE", format_cbor=True)
)
Note: use cbor format for production applications. Pass format_cbor=False only to simplify
development initially as the answer will be provided as json.
Additionally, you can use openai embeddings:
client.llm_embed(
request=LLMEmbeddingRequest(
texts=["Sky is blue."],
profile=Profile.OPENAI_EMBEDDING_LARGE,
dimensions=768
)
)
Question Answering
You can run question answering against corpus of documents, like documentation or Youtrack issues.
response = ""
for chunk in grazie_api.answer_stream(
query="How to write a coroutine?",
data_source="kotlin_1.9.23"
):
if chunk.chunk.summaryChunk:
response += chunk.chunk.summaryChunk
You can find the list of available data sources on https://try.ai.intellij.net/qa
Plain Retrieval
You can also run question answering against a corpus of documents, retrieving only raw documents:
client.retrieve(
query="How to change a font size in Fleet?",
data_source="jetbrains-fleet-1.36",
profile=Profile.OPENAI_GPT_4_TURBO,
size=10,
)
Or providing a list of prioritized data sources:
client.retrieve_v2(
query="How to change a font size in Fleet?",
config_name="fleet-ide",
data_source_lists=[
[
PrioritizedSource(name="jetbrains-fleet-1.45", priority=0),
PrioritizedSource(name="jetbrains-fleet-1.46", priority=1),
]
],
profile=Profile.OPENAI_GPT_4_TURBO,
size=10,
)
Grazie Api Gateway Client V2
The api client V2 for JetBrains AI Platform.
Implemented features
Basic usage
Client is available in two flavours APIGatewayClient and AsyncAPIGatewayClient.
ApiGatewayClient
import os
from grazie.api.client_v2 import APIGatewayClient, GatewayEndpoint
api_key = os.getenv("GRAZIE_JWT_TOKEN")
client = APIGatewayClient(
api_key=api_key,
endpoint=GatewayEndpoint.STAGING,
)
# Fetch all available tasks in TaskAPI
print(client.tasks.roster())
AsyncApiGatewayClient
import asyncio
import os
from grazie.api.client_v2 import AsyncAPIGatewayClient, GatewayEndpoint
async def main():
api_key = os.getenv("GRAZIE_JWT_TOKEN")
client = AsyncAPIGatewayClient(
api_key=api_key,
endpoint=GatewayEndpoint.STAGING,
)
# Fetch all available tasks in TaskAPI
print(await client.tasks.roster())
asyncio.run(main())
TaskAPI
Execute predefined task (one line python code completion)
from grazie.api.client_v2 import APIGatewayClient
from grazie.api.client_v2.tasks.code import CodeOneLinePythonJetCompleteTask
client = APIGatewayClient()
client.tasks.execute(
CodeOneLinePythonJetCompleteTask.default(
prefix="import js",
suffix="\n",
)
)
Execute custom task
from grazie.api.client_v2 import APIGatewayClient
from grazie.api.client_v2.tasks.types import TaskCall
client = APIGatewayClient()
client.tasks.execute(
TaskCall(
id="code-generate:default",
parameters=dict(
instructions="Write me a simple python script",
prefix="",
suffix="",
language="python",
)
)
)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file grazie_api_gateway_client-0.1.17.tar.gz.
File metadata
- Download URL: grazie_api_gateway_client-0.1.17.tar.gz
- Upload date:
- Size: 49.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.8.20 Linux/5.15.0-1081-aws
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ec36ec646fed32512d9a4e175bc1a1da2492e09d78436e1f40feef55d9acadeb
|
|
| MD5 |
b2df5d3c9f5520d7e9f705f3082bf1d5
|
|
| BLAKE2b-256 |
b562481242140a94cc7b34afe806389b6df7515597e815b1531376affadbf64a
|
File details
Details for the file grazie_api_gateway_client-0.1.17-py3-none-any.whl.
File metadata
- Download URL: grazie_api_gateway_client-0.1.17-py3-none-any.whl
- Upload date:
- Size: 29.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.8.20 Linux/5.15.0-1081-aws
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ba91f0074cb589aaf37cf7dbf505b9309a739d88c1cb1faf5ee16a322eeaa5d1
|
|
| MD5 |
419293578c1b9acb3610fd8db01e4a29
|
|
| BLAKE2b-256 |
7210ec03864c8f4891e5a620a2ef020147c8b1a7bcf97a1e2a6e1fa8561a496b
|