Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
.idea
.vscode
.run
genai-env.env
genai-env.sh

# File-based project format:
*.iml
Expand All @@ -23,3 +21,6 @@ __pycache__/

# Coverage
.coverage

# local environment variables
.local_env/
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Git branch - [main](https://github.com/Stratio/genai-examples)

* Examples built with genai-core 0.7.0

## Git branch - [branch-0.6](https://github.com/Stratio/genai-examples/tree/branch-0.6)

* Examples built with genai-core 0.6.1

## Git branch - [branch-0.5](https://github.com/Stratio/genai-examples/tree/branch-0.5)
Expand Down
4 changes: 2 additions & 2 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ hose {
DEVTIMEOUT = 60
RELEASETIMEOUT = 60
BUILDTOOL = 'make'
BUILDTOOL_IMAGE = 'stratio/python-builder-3.11:1.2.0'
BUILDTOOL_IMAGE = 'stratio/python-builder-3.11:1.3.3'
BUILDTOOL_CPU_LIMIT = '8'
BUILDTOOL_CPU_REQUEST = '2'
PYTHON_MODULE = true
Expand All @@ -20,7 +20,7 @@ hose {
conf: config,
sonarAdditionalProperties: [
"sonar.language": "py",
"sonar.python.version": "3.9",
"sonar.python.version": "3.11",
"sonar.sources": ".",
"sonar.exclusions": "*/tests/**,*/scripts/**,*/pytest-coverage.xml",
"sonar.tests": ".",
Expand Down
80 changes: 31 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ This repository is intended for developers who want to create GenAI chains using

| Git branch | GenAI Core version |
|-------------------------------------------------------------------------|--------------------|
| main | 0.6.1 |
| main | 0.7.0 |
| [branch-0.6](https://github.com/Stratio/genai-examples/tree/branch-0.6) | 0.6.1 |
| [branch-0.5](https://github.com/Stratio/genai-examples/tree/branch-0.5) | 0.5.1 |
| [branch-0.4](https://github.com/Stratio/genai-examples/tree/branch-0.4) | 0.4.0 |
| [branch-0.3](https://github.com/Stratio/genai-examples/tree/branch-0.3) | 0.3.4 |
Expand All @@ -21,17 +22,15 @@ Please check the readme of each chain for more information.

Stratio GenAI chains are built with [Poetry](https://python-poetry.org/docs/#installation), so in order to develop a chain you need to make sure you have the following tools in your machine:

* [Python](https://www.python.org/) >= 3.9 (GenAI-API uses Python 3.12 to deploy the chain)
* [Python](https://www.python.org/) >= 3.11 (GenAI-API uses Python 3.12 to deploy the chain)
* [Poetry](https://python-poetry.org/docs/#installing-with-the-official-installer) >= 2.2 (We recommend to use the "official installer method".)
* A Python editor of you choice, like [PyCharm](https://www.jetbrains.com/pycharm/) or [Visual Studio Code](https://code.visualstudio.com/)

## Quick start guide (TLDR)

### *Stratio GenAI Developer Proxy*

You need access to *Stratio GenAI Developer Proxy* service. This service allows you to install the *Stratio GenAI Core* dependency and to access the services running in the Development environment from your local machine. The service is installed by Operations team. They will provide you a URL like this `https://genai-developer-proxy-loadbalancer.your-tenant-genai.yourdomain.com:8080`.

> **WARNING**: Make sure you use the Load Balancer URL, not the KEOS Ingress URL!
You need access to *Stratio GenAI Developer Proxy* service. This service allows you to install the *Stratio GenAI Core* dependency and to access the services running in the Development environment from your local machine. The service is installed by Operations team. They will provide you a URL like this `https://genai.your-tenant.yourdomain.com/genai-developer-proxy`.

### User certificate

Expand Down Expand Up @@ -68,12 +67,12 @@ $ poetry install
Execute the following commands to configure the environment variables:

```
$ python ../scripts/create_env_file.py \
$ poetry run local-env \
--certs_path /path/to/certs \
--proxy_url https://genai-developer-proxy-loadbalancer.your-tenant-genai.yourdomain.com:8080
--developer_proxy_url https://genai.your-tenant.yourdomain.com/genai-developer-proxy
```

You will find the files `genai-env.env` and `genai-env.sh` in the `genai-examples/scripts` folder with the environment variables.
You will find the files `genai-env.env` and `genai-env.sh` in the `.local_env` folder with the environment variables.

### Run the chain

Expand All @@ -86,7 +85,7 @@ If you use PyCharm, follow [those steps](#running-from-pycharm) to run the chain
Source the environment variables and launch the `main.py` script in the Poetry environment to run the chain locally:

```
$ source ../scripts/genai-env.sh
$ source .local_env/genai-env.sh
$ poetry run python basic_actor_chain/main.py
```

Expand Down Expand Up @@ -116,7 +115,7 @@ When your chain is ready, you can [build](#building-your-chain) and [deploy](#de

Stratio GenAI chains are intended to run inside *Stratio GenAI API*. In order to try out your chain from your local machine while developing, *Stratio GenAI Core* provides a GenAI-API-like standalone server that can be started in your computer to serve your chain just as if it was deployed in *Stratio GenAI API*, exposing the `invoke` endpoint and a Swagger UI. All the examples in this repository contain a `main.py` file, used to run the chain locally, where you can see how to start this standalone server. The constructor of this standalone server receives as an argument the same configuration that would be passed in the request body when registering the chain in *Stratio GenAI API*. However, you will need some extra setup to be able to run chains in your local computer. The main problems are:

1. Since the chains run inside *Stratio GenAI API*, they can connect to several services, like *Stratio GenAI Gateway*, *Stratio Virtualizer*, Opensearch or *Stratio GenAI API*. Moreover, they will be able to use the *Stratio GenAI API* credentials to connect to them. The problem is that these services are not usually exposed outside the cluster, and if they were, you would need the correct *Stratio GenAI API* credentials, which may be not available to you.
1. Since the chains run inside *Stratio GenAI API*, they can connect to several services, like *Stratio GenAI LiteLLM*, *Stratio Virtualizer*, Opensearch or *Stratio GenAI API*. Moreover, they will be able to use the *Stratio GenAI API* credentials to connect to them. The problem is that these services are not usually exposed outside the cluster, and if they were, you would need the correct *Stratio GenAI API* credentials, which may be not available to you.

2. Running inside *Stratio GenAI API* in the cluster, the chains have access to Vault, but it won't be accessible from your local machine.

Expand All @@ -128,15 +127,15 @@ Stratio GenAI chains are intended to run inside *Stratio GenAI API*. In order to

The first and last problems above are solved by the *Stratio GenAI Developer Proxy*, which can be deployed in the development cluster to provide access to the services running inside the cluster, as well as to provide access to the PyPi server inside *Stratio GenAI API* containing the *Stratio GenAI Core* dependency.

Once the service in installed on your development cluster, it should be reachable through a URL like this: `https://genai-developer-proxy-loadbalancer.your-tenant-genai.yourdomain.com:8080`. Then, under different paths on that server, you can access the different services. For instance, the *Stratio GenAI Gateway* would be accessed from your local machine through `https://genai-developer-proxy-loadbalancer.your-tenant-genai.yourdomain.com:8080/service/genai-gateway`. All the available services, with their path in *Stratio GenAI Developer Proxy* are listed in the following table:
| service | Developer proxy path |
|-------------------------|-------------------------------------|
| *Stratio GenAI Gateway* | `/service/genai-gateway` |
| *Stratio GenAI API* | `/service/genai-api` |
| *Stratio Virtualizer* | `/service/virtualizer` |
| Opensearch | `/service/opensearch` |
| *Stratio Governance* | `/service/governance` |
| PyPi server | `/service/genai-api/v1/pypi/simple` |
Once the service is installed on your development cluster, it should be reachable through a URL like this: `https://genai-developer-proxy-loadbalancer.your-tenant-genai.yourdomain.com:8080`. Then, under different paths on that server, you can access the different services. For instance, the *Stratio GenAI LiteLLM* would be accessed from your local machine through `https://genai-developer-proxy-loadbalancer.your-tenant-genai.yourdomain.com:8080/service/genai-litellm`. All the available services, with their path in *Stratio GenAI Developer Proxy* are listed in the following table:
| service | Developer proxy path |
|--------------------------|-------------------------------------|
| *Stratio GenAI LiteLLM* | `/service/genai-litellm` |
| *Stratio GenAI API* | `/service/genai-api` |
| *Stratio Virtualizer* | `/service/virtualizer` |
| Opensearch | `/service/opensearch` |
| *Stratio Governance* | `/service/governance` |
| PyPi server | `/service/genai-api/v1/pypi/simple` |

Note that, depending on how the *Stratio GenAI Developer Proxy* is configured on the development cluster, some of these services may be not exposed.

Expand Down Expand Up @@ -189,19 +188,19 @@ You can obtain your user certificates from *Stratio GoSec*:

### Extra environment variables

Some clients provided in *Stratio GenAI Core*, like the *Stratio GenAI Gateway* client or the *Stratio GenAI API* client, obtain their certificates by reading some environment variables instead of accessing Vault. Also, they expect some other environment variables to be defined. In a "normal" chain execution inside *Stratio GenAI API*, these variables are already set in the *Stratio GenAI API* container. If your chain uses these clients, you should set the corresponding environment variables to run it locally.
Some clients provided in *Stratio GenAI Core*, like the *Stratio GenAI LiteLLM* client or the *Stratio GenAI API* client, obtain their certificates by reading some environment variables instead of accessing Vault. Also, they expect some other environment variables to be defined. In a "normal" chain execution inside *Stratio GenAI API*, these variables are already set in the *Stratio GenAI API* container. If your chain uses these clients, you should set the corresponding environment variables to run it locally.

For your convenience, we provide a script that generates two files with all the needed variables by just giving it the path to your certificates folder and the host of the *Stratio GenAI Developer Proxy*. Then, you can add it to PyCharm or source it in your terminal.

Execute the following command to configure the environment variables:

```
$ python scripts/create_env_file.py \
$ poetry run local-env \
--certs_path /path/to/certs \
--proxy_url https://genai-developer-proxy-loadbalancer.your-tenant-genai.yourdomain.com:8080
--developer_proxy_url https://genai.your-tenant.yourdomain.com/genai-developer-proxy
```

You will find the files `genai-env.env` and `genai-env.sh` in the `genai-examples/scripts` folder with the environment variables.
You will find the files `genai-env.env` and `genai-env.sh` in the `.local_env` folder with the environment variables.

### *Stratio GenAI Core* dependency

Expand Down Expand Up @@ -256,23 +255,13 @@ Now you can run your chain (note that the script should be called with `poetry r
$ poetry run python main.py
```

This may fail if your `main.py` script or your chain expects some environment variables to be defined in order to work properly, like those pointing to your local certificates for the Vault or the Gateway client, as we explained above. If that is the case, you must set the needed environment variables before calling the script. Here we show an example setting the variables needed to [mock Vault](#user-certificates-and-vault-client-development-mode), but you can check all the variables that might be needed by generating the `.sh` file with the [helper script](#extra-environment-variables), or just using that generated file.

Create a file env.sh exporting the needed variables,
```bash
export VAULT_LOCAL_CLIENT_CERT="/path/to/cert.crt"
export VAULT_LOCAL_CLIENT_KEY="/path/to/private-key.key"
export VAULT_LOCAL_CA_CERTS="/path/to/ca-cert.crt"
This may fail if your `main.py` script or your chain expects some environment variables to be defined in order to work properly, like those pointing to your local certificates for the Vault or the Gateway client, as we explained above. If that is the case, you must source the environment file generated by the [helper script](#extra-environment-variables) before calling the script:

# add here whatever other variables might be needed
# ...
# ...
```
source it
```
$ source env.sh
$ source .local_env/genai-env.sh
$ poetry run python main.py
```
and run the script again. You will see the logs in the terminal, and you can open the web interface of the chain in the browser: http://127.0.0.1:8080.
and you will see the logs in the terminal, and you can open the web interface of the chain in the browser: http://127.0.0.1:8080.

#### Running from PyCharm

Expand All @@ -288,18 +277,11 @@ Execute the chain on PyCharm.

* Right-click on the `main.py` file => Run 'main'

This may fail if your `main.py` script or your chain expects some environment variables to be defined in order to work properly, like those pointing to your local certificates for the Vault or the Gateway client, as we explained above. If that is the case, you must set the needed environment variables before calling the script. Here we show an example setting the variables needed to [mock Vault](#user-certificates-and-vault-client-development-mode), but you can check all the variables that might be needed by generating the `.env` file with the [helper script](#extra-environment-variables), or just using that generated file.
This may fail if your `main.py` script or your chain expects some environment variables to be defined in order to work properly, like those pointing to your local certificates for the Vault or the Gateway client, as we explained above. If that is the case, you must add the environment file generated by the [helper script](#extra-environment-variables) to the `main.py` run configuration in PyCharm.

Create a file genai.env:
```
VAULT_LOCAL_CLIENT_CERT=/path/to/cert.crt
VAULT_LOCAL_CLIENT_KEY=/path/to/private-key.key
VAULT_LOCAL_CA_CERTS=/path/to/ca-cert.crt
ADD_HERE_OTHER_VARIABLES=that_might_be_needed
```
and add it to the `main.py` run configuration in PyCharm:
Add the `.local_env/genai-env.env` file to the `main.py` run configuration in PyCharm:

* Edit Configurations => Path to ".env" files => Select the `genai.env` file in the `genai-examples/scripts` folder.
* Edit Configurations => Path to ".env" files => Select the `genai-env.env` file in the `.local_env` folder.

![](./docs/pycharm_list_configurations.png)

Expand Down Expand Up @@ -361,11 +343,11 @@ curl -X 'POST' \
-d '{
"chain_id": "basic_actor_chain",
"chain_config": {
"package_id": "basic_actor_chain-0.3.4a0",
"package_id": "basic_actor_chain-0.7.0a0",
"chain_module": "basic_actor_chain.chain",
"chain_class": "BasicActorChain",
"chain_params": {
"gateway_endpoint": "openai-chat",
"gateway_endpoint": "openai-gpt-5.4-mini",
"llm_timeout": 60
}
}
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.6.0-SNAPSHOT
0.7.0-SNAPSHOT
27 changes: 8 additions & 19 deletions example-chain-basic-actor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This is an example showing how to define a simple Actor and use it in a basic qu

To set up the chain locally, follow the steps in the [main README of this repository](../README.md). Here is a summary of the steps:

1. Make sure you have Python >= 3.9 (GenAI-API uses Python 3.12 to deploy the chain) and Poetry >= 2.2 installed.
1. Make sure you have Python >= 3.11 (GenAI-API uses Python 3.12 to deploy the chain) and Poetry >= 2.2 installed.

2. Edit the `pyproject.toml` and change the URL of the `stratio-releases` repository. You should use the URL of the *Stratio GenAI Developer Proxy* Load Balancer including path "/service/genai-api/v1/pypi/simple".

Expand All @@ -26,14 +26,10 @@ $ poetry lock
$ poetry install
```

4. Configure the environment variables executing the script `scripts/create_env_file.py`. You will find the environment variables in the files `genai-env.env` and `genai-env.sh` in the `genai-examples/scripts` folder. This chain uses the following environment variables:
4. Configure the environment variables running the `local-env` Poetry script. You will find the environment variables in the files `.local_env/genai-env.env` and `.local_env/genai-env.sh`.

```bash
GENAI_GATEWAY_URL=https://genai-developer-proxy-loadbalancer.your-tenant-genai.yourdomain.com:8080/service/genai-gateway
GENAI_GATEWAY_USE_SSL=true
GENAI_GATEWAY_CLIENT_CERT=/path/to/certs/user.crt
GENAI_GATEWAY_CLIENT_KEY=/path/to/certs/user_private.key
GENAI_GATEWAY_CA_CERTS=/path/to/certs/ca-cert.crt
poetry run local-env --certs_path /path/to/certs --developer_proxy_url https://genai.your-tenant.yourdomain.com/genai-developer-proxy
```

5. Run the chain `basic_actor_chain/main.py`. You can do it in the terminal or in PyCharm. You can open the Swagger UI in the URL `http://127.0.0.1:8080/`.
Expand All @@ -42,25 +38,18 @@ GENAI_GATEWAY_CA_CERTS=/path/to/certs/ca-cert.crt
poetry run python basic_actor_chain/main.py
```

6. Invoke the chain using the `POST /invoke` endpoint with the following request body. Replace `<your-user>` and `<your-tenant>` with your user and tenant:
6. Invoke the chain using the `POST /invoke` endpoint with the following request body:

```json
{
"input": {
"user_request": "Hi! Nice to meet you! Where is the Queen of Hearts?"
},
"config": {
"metadata": {
"__genai_state": {
"client_auth_type": "mtls",
"client_user_id": "<your-user>",
"client_tenant": "<your-tenant>"
}
}
}
}
```

> The user credentials are automatically injected from the environment variables configured by the `local-env` script in step 4. No need to include them in the request body.

## Deployment in the Stratio GenAI API

To deploy the chain in the Stratio GenAI API, follow the steps in the [main README of this repository](../README.md). Here is a summary of the steps:
Expand All @@ -74,11 +63,11 @@ To deploy the chain in the Stratio GenAI API, follow the steps in the [main READ
{
"chain_id": "basic_actor_chain",
"chain_config": {
"package_id": "basic_actor_chain-0.6.0a0",
"package_id": "basic_actor_chain-0.7.0a0",
"chain_module": "basic_actor_chain.chain",
"chain_class": "BasicActorChain",
"chain_params": {
"gateway_endpoint": "openai-chat-gpt-4.1-mini",
"gateway_endpoint": "openai-gpt-5.4-mini",
"llm_timeout": 60
}
}
Expand Down
Loading