grazie_api_gateway_client


Namegrazie_api_gateway_client JSON
Version 0.3.3 PyPI version JSON
download
home_pageNone
SummaryApi client for Grazie services
upload_time2025-07-28 13:48:33
maintainerNone
docs_urlNone
authorJetBrains
requires_python>=3.8
licenseApache-2.0
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Grazie Api Gateway Client

> Note, this package is deprecated, please refer to [Grazie Api Gateway Client V2](#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.

```shell
poetry run -C libs/grazie_api_gateway_client python3 -m grazie.api.client -p openai-gpt-4 chat -v 8 '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:

```python
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:

### Profiles
List all available LLM profiles:

```python
print(client.v8.profiles())
```

### Completion
Without suffix:

```python
client.v8.complete(
    prompt=CompletionPrompt(
        prefix="Once upon a time there was a unicorn. ",
    ),
    profile=Profile.GRAZIE_CHAT_LLAMA_V2_7b,
)
```

With suffix:

```python
client.v8.complete(
    prompt=CompletionPrompt(
        prefix="Once upon a time there was a unicorn. ",
        suffix=" And they lived happily ever after!"
    ),
    profile=Profile.GRAZIE_CHAT_LLAMA_V2_7b,
)
```

### Chat

```python
client.v8.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.

```python
client.v8.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.

```python
response = ""
for chunk in client.v8.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.content
```

#### Tool use

Here's an example of the tool usage workflow. For more information, please see the
[documentation](https://platform.stgn.jetbrains.ai/docs/tool-use)

```python
geo_tool = (
    ToolDefinition(
        name="current_temperature",
        description="Get the current temperature for the given location",
    )
    .add_parameter(
        name="latitude",
        description="The latitude of the location",
        _type=ToolDefinition.ToolParameterTypes.STRING,
        required=True,
    )
    .add_parameter(
        name="longitude",
        description="The longitude of the location",
        _type=ToolDefinition.ToolParameterTypes.STRING,
        required=True,
    )
)

chat_response = client.v8.chat(
    prompt_id="tool_call",
    profile=Profile.OPENAI_CHAT_GPT,
    chat=ChatPrompt()
    .add_system("You are an assistant that uses tools to answer user questions accurately.")
    .add_user("What is the current temperature in Amsterdam?"),
    parameters={
        LLMParameters.Tools: Parameters.JsonValue.from_tools(geo_tool),
        LLMParameters.ToolChoiceRequired: Parameters.BooleanValue(True),
    },
)

content = chat_response.content
tool = chat_response.responses[0].tool_calls[0]

url_params = "&".join(f"{key}={value}" for key, value in json.loads(content).items())
url_params = "&".join([url_params, "current=temperature"])
# The final URL should look like
#   https://api.open-meteo.com/v1/forecast?latitude=52.3676&longitude=4.9041&current=temperature

meteo_response = requests.get(f"https://api.open-meteo.com/v1/forecast?{url_params}").text

final_response = client.v8.chat(
    prompt_id="tool_call",
    profile=Profile.OPENAI_CHAT_GPT,
    chat=ChatPrompt()
    .add_user("What is the current temperature in Amsterdam?")
    .add_tool(
        id=tool.id,
        tool_name=tool.name,
        content=tool.content,
        result=meteo_response,
    ),
    parameters={
        LLMParameters.Tools: Parameters.JsonValue.from_tools(geo_tool),
    },
)

print(final_response.content)
```

### 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

* [Tasks](#TaskAPI)

## Basic usage

Client is available in two flavours `APIGatewayClient` and `AsyncAPIGatewayClient`.

### ApiGatewayClient
```python
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
```python
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

Please refer to the `client.tasks.roster()` for the list of available task IDs.
The roster output is in the format of `<task-id>:<task-tag>`

See the [Swagger page](https://api.app.stgn.grazie.aws.intellij.net/swagger-ui/index.html?urls.primaryName=Tasks)
to find parameters for the specific task.

### Execute a task

```python
from grazie.api.client_v2 import APIGatewayClient

client = APIGatewayClient()
client.tasks.execute(
    id="code-generate:default",
    parameters=dict(
        instructions="Write me a simple python script",
        prefix="",
        suffix="",
        language="python",
    )
)
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "grazie_api_gateway_client",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": null,
    "author": "JetBrains",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/6c/76/f80883e178e78c71aafc553c6c8bb35e339fec478820149826737f5ae896/grazie_api_gateway_client-0.3.3.tar.gz",
    "platform": null,
    "description": "# Grazie Api Gateway Client\n\n> Note, this package is deprecated, please refer to [Grazie Api Gateway Client V2](#Grazie-Api-Gateway-Client-V2) first and check if the new client library supports functionality you need.\n\nThis package provides api client for JetBrains AI Platform llm functionality.\nSupported methods are chat, completion and embeddings.\n\nSupport for Grazie NLP services is planned in the future.\n\nYou can try models in the browser by going to https://try.ai.intellij.net/\nor using the command-line interface.\n\n```shell\npoetry run -C libs/grazie_api_gateway_client python3 -m grazie.api.client -p openai-gpt-4 chat -v 8 'Who was the most famous pop star in the 90s?'\n```\n\n## Usage\n\nFirst you have to create an instance of client,\nplease check class documentation to know more about parameters:\n\n```python\nclient = GrazieApiGatewayClient(\n    grazie_agent=GrazieAgent(name=\"grazie-api-gateway-client-readme\", version=\"dev\"),\n    url=GrazieApiGatewayUrls.STAGING,\n    auth_type=AuthType.USER,\n    grazie_jwt_token=***\n)\n```\n\nBelow are examples of usage by method:\n\n### Profiles\nList all available LLM profiles:\n\n```python\nprint(client.v8.profiles())\n```\n\n### Completion\nWithout suffix:\n\n```python\nclient.v8.complete(\n    prompt=CompletionPrompt(\n        prefix=\"Once upon a time there was a unicorn. \",\n    ),\n    profile=Profile.GRAZIE_CHAT_LLAMA_V2_7b,\n)\n```\n\nWith suffix:\n\n```python\nclient.v8.complete(\n    prompt=CompletionPrompt(\n        prefix=\"Once upon a time there was a unicorn. \",\n        suffix=\" And they lived happily ever after!\"\n    ),\n    profile=Profile.GRAZIE_CHAT_LLAMA_V2_7b,\n)\n```\n\n### Chat\n\n```python\nclient.v8.chat(\n    chat=ChatPrompt()\n        .add_system(\"You are a helpful assistant.\")\n        .add_user(\"Who won the world series in 2020?\"),\n    profile=Profile.OPENAI_CHAT_GPT\n)\n```\n\nAdditionally you can pass id of your prompt or feature via `prompt_id` parameter.\nThis identifier can later be used to check spending and calculate price of the feature per user or\nper call.\n\nIf you develop prompt which should answer in a structured format (i.e. JSON) it's better to pass\ntemperature = 0.\nThis makes generation deterministic (almost) and will provide parsable responses more reliably.\n\n```python\nclient.v8.chat(\n    chat=ChatPrompt()\n        .add_system(\"You are a helpful assistant.\")\n        .add_user(\"Who won the world series in 2020?\"),\n    profile=Profile.OPENAI_CHAT_GPT,\n    parameters={\n        LLMParameters.Temperature: Parameters.FloatValue(0.0)\n    }\n)\n```\n\nNote: this parameter is currently only supported for OpenAI models.\n\n#### Streaming\n\nOutputs from chat models can be slow, to show progress to a user you can call chat_stream.\nThe output would be a stream of text chunks.\n\n```python\nresponse = \"\"\nfor chunk in client.v8.chat_stream(\n    chat=ChatPrompt()\n        .add_user(\"Who won the world series in 2020?\")\n        .add_assistant(\"The Los Angeles Dodgers won the World Series in 2020.\")\n        .add_user(\"Where was it played? Write a small poem about it!\"),\n    profile=Profile.OPENAI_CHAT_GPT\n):\n    response += chunk.content\n```\n\n#### Tool use\n\nHere's an example of the tool usage workflow. For more information, please see the\n[documentation](https://platform.stgn.jetbrains.ai/docs/tool-use)\n\n```python\ngeo_tool = (\n    ToolDefinition(\n        name=\"current_temperature\",\n        description=\"Get the current temperature for the given location\",\n    )\n    .add_parameter(\n        name=\"latitude\",\n        description=\"The latitude of the location\",\n        _type=ToolDefinition.ToolParameterTypes.STRING,\n        required=True,\n    )\n    .add_parameter(\n        name=\"longitude\",\n        description=\"The longitude of the location\",\n        _type=ToolDefinition.ToolParameterTypes.STRING,\n        required=True,\n    )\n)\n\nchat_response = client.v8.chat(\n    prompt_id=\"tool_call\",\n    profile=Profile.OPENAI_CHAT_GPT,\n    chat=ChatPrompt()\n    .add_system(\"You are an assistant that uses tools to answer user questions accurately.\")\n    .add_user(\"What is the current temperature in Amsterdam?\"),\n    parameters={\n        LLMParameters.Tools: Parameters.JsonValue.from_tools(geo_tool),\n        LLMParameters.ToolChoiceRequired: Parameters.BooleanValue(True),\n    },\n)\n\ncontent = chat_response.content\ntool = chat_response.responses[0].tool_calls[0]\n\nurl_params = \"&\".join(f\"{key}={value}\" for key, value in json.loads(content).items())\nurl_params = \"&\".join([url_params, \"current=temperature\"])\n# The final URL should look like\n#   https://api.open-meteo.com/v1/forecast?latitude=52.3676&longitude=4.9041&current=temperature\n\nmeteo_response = requests.get(f\"https://api.open-meteo.com/v1/forecast?{url_params}\").text\n\nfinal_response = client.v8.chat(\n    prompt_id=\"tool_call\",\n    profile=Profile.OPENAI_CHAT_GPT,\n    chat=ChatPrompt()\n    .add_user(\"What is the current temperature in Amsterdam?\")\n    .add_tool(\n        id=tool.id,\n        tool_name=tool.name,\n        content=tool.content,\n        result=meteo_response,\n    ),\n    parameters={\n        LLMParameters.Tools: Parameters.JsonValue.from_tools(geo_tool),\n    },\n)\n\nprint(final_response.content)\n```\n\n### Embeddings\n\nYou can also use api to build float vector embeddings for sentences and texts.\n\n```\nclient.embed(\n    request=EmbeddingRequest(texts=[\"Sky is blue.\"], model=\"sentence-transformers/LaBSE\", format_cbor=True)\n)\n```\n\nNote: use cbor format for production applications. Pass `format_cbor=False` only to simplify\ndevelopment initially as the answer will be provided as json.\n\nAdditionally, you can use openai embeddings:\n\n```\nclient.llm_embed(\n    request=LLMEmbeddingRequest(\n        texts=[\"Sky is blue.\"],\n        profile=Profile.OPENAI_EMBEDDING_LARGE,\n        dimensions=768\n    )\n)\n```\n\n### Question Answering\n\nYou can run question answering against corpus of documents, like documentation or Youtrack issues.\n\n```\nresponse = \"\"\nfor chunk in grazie_api.answer_stream(\n    query=\"How to write a coroutine?\", \n    data_source=\"kotlin_1.9.23\"\n):\n    if chunk.chunk.summaryChunk:\n        response += chunk.chunk.summaryChunk\n```\n\nYou can find the list of available data sources on https://try.ai.intellij.net/qa\n\n#### Plain Retrieval\n\nYou can also run question answering against a corpus of documents, retrieving only raw documents:\n\n```\nclient.retrieve(\n    query=\"How to change a font size in Fleet?\",\n    data_source=\"jetbrains-fleet-1.36\",\n    profile=Profile.OPENAI_GPT_4_TURBO,\n    size=10,\n)\n```\n\nOr providing a list of prioritized data sources:\n\n```\nclient.retrieve_v2(\n    query=\"How to change a font size in Fleet?\",\n    config_name=\"fleet-ide\",\n    data_source_lists=[\n        [\n            PrioritizedSource(name=\"jetbrains-fleet-1.45\", priority=0), \n            PrioritizedSource(name=\"jetbrains-fleet-1.46\", priority=1), \n        ]\n    ],\n    profile=Profile.OPENAI_GPT_4_TURBO,\n    size=10,\n)\n```\n\n\n# Grazie Api Gateway Client V2\n\nThe api client V2 for JetBrains AI Platform.\n\n## Implemented features\n\n* [Tasks](#TaskAPI)\n\n## Basic usage\n\nClient is available in two flavours `APIGatewayClient` and `AsyncAPIGatewayClient`.\n\n### ApiGatewayClient\n```python\nimport os\n\nfrom grazie.api.client_v2 import APIGatewayClient, GatewayEndpoint\n\napi_key = os.getenv(\"GRAZIE_JWT_TOKEN\")\nclient = APIGatewayClient(\n    api_key=api_key,\n    endpoint=GatewayEndpoint.STAGING,\n)\n\n# Fetch all available tasks in TaskAPI\nprint(client.tasks.roster())\n```\n\n### AsyncApiGatewayClient\n```python\nimport asyncio\nimport os\n\nfrom grazie.api.client_v2 import AsyncAPIGatewayClient, GatewayEndpoint\n\n\nasync def main():\n    api_key = os.getenv(\"GRAZIE_JWT_TOKEN\")\n    client = AsyncAPIGatewayClient(\n        api_key=api_key,\n        endpoint=GatewayEndpoint.STAGING,\n    )\n\n    # Fetch all available tasks in TaskAPI\n    print(await client.tasks.roster())\n\nasyncio.run(main())\n```\n\n## TaskAPI\n\nPlease refer to the `client.tasks.roster()` for the list of available task IDs.\nThe roster output is in the format of `<task-id>:<task-tag>`\n\nSee the [Swagger page](https://api.app.stgn.grazie.aws.intellij.net/swagger-ui/index.html?urls.primaryName=Tasks)\nto find parameters for the specific task.\n\n### Execute a task\n\n```python\nfrom grazie.api.client_v2 import APIGatewayClient\n\nclient = APIGatewayClient()\nclient.tasks.execute(\n    id=\"code-generate:default\",\n    parameters=dict(\n        instructions=\"Write me a simple python script\",\n        prefix=\"\",\n        suffix=\"\",\n        language=\"python\",\n    )\n)\n```\n",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "Api client for Grazie services",
    "version": "0.3.3",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "95f2f619b27860ddfaca964bb18e8bbc3aaae1d56df638d200da3cdc1d5759c7",
                "md5": "938381ee3135f7369f27c536187f75d1",
                "sha256": "67cec74509ed7dd32c43588315f1c8b80ac29c203592f0d28a385b0ff1845bc8"
            },
            "downloads": -1,
            "filename": "grazie_api_gateway_client-0.3.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "938381ee3135f7369f27c536187f75d1",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 37485,
            "upload_time": "2025-07-28T13:48:31",
            "upload_time_iso_8601": "2025-07-28T13:48:31.832298Z",
            "url": "https://files.pythonhosted.org/packages/95/f2/f619b27860ddfaca964bb18e8bbc3aaae1d56df638d200da3cdc1d5759c7/grazie_api_gateway_client-0.3.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6c76f80883e178e78c71aafc553c6c8bb35e339fec478820149826737f5ae896",
                "md5": "7d8f89172562f1bde4a79e5568baca2c",
                "sha256": "29a3f0ce7d185b79a479bb83631f2790d924cefb3293748aa0fc3e032fab0d74"
            },
            "downloads": -1,
            "filename": "grazie_api_gateway_client-0.3.3.tar.gz",
            "has_sig": false,
            "md5_digest": "7d8f89172562f1bde4a79e5568baca2c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 26586,
            "upload_time": "2025-07-28T13:48:33",
            "upload_time_iso_8601": "2025-07-28T13:48:33.088889Z",
            "url": "https://files.pythonhosted.org/packages/6c/76/f80883e178e78c71aafc553c6c8bb35e339fec478820149826737f5ae896/grazie_api_gateway_client-0.3.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-07-28 13:48:33",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "grazie_api_gateway_client"
}
        
Elapsed time: 1.94544s