Skip to content

Commit 51a39f2

Browse files
authored
Portkey intergration (#221)
* Dep to portkey pyproject #219 Signed-off-by: Armand <arm.gilles@gmail.com> * Implémentation de portkey pour le monitoring du LLM #219 Signed-off-by: Armand <arm.gilles@gmail.com> * Update readme to add LLM section Signed-off-by: Armand <arm.gilles@gmail.com> * Add user for monitoring #219 Signed-off-by: Armand <arm.gilles@gmail.com> * Export secret for CI #219 Signed-off-by: Armand <arm.gilles@gmail.com> * Agent monitor for monitor purpose is now an arg of create_chat to difference dev and front app next #219 Signed-off-by: Armand <arm.gilles@gmail.com> * Add another check for calculation distance CI Signed-off-by: Armand <arm.gilles@gmail.com> --------- Signed-off-by: Armand <arm.gilles@gmail.com>
1 parent d8f3fe3 commit 51a39f2

File tree

5 files changed

+39
-16
lines changed

5 files changed

+39
-16
lines changed

.github/workflows/CI.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ jobs:
2929
touch .env
3030
echo KEY_API_BDX=${{ secrets.KEY_API_BDX }} >> .env
3131
echo MISTRAL_API_KEY=${{ secrets.MISTRAL_API_KEY }} >> .env
32+
echo PORTKEY_API_KEY=${{ secrets.PORTKEY_API_KEY }} >> .env
33+
echo PORTKEY_VIRTUAL_KEY=${{ secrets.PORTKEY_VIRTUAL_KEY }} >> .env
3234
- name: Copy test data
3335
run: |
3436
cp -r ${{ env.ROOT_TESTS_DATA }}/* ${{ github.workspace }}/tests/

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ Créer un .env à la racine du projet avec :
5050
- MAPBOX_TOKEN="YOUR TOKEN HERE" (pour l'utilisation des graphiques avec mapbox).
5151
- KEY_API_BDX="YOUR KEY HERE" (pour l'utilisation de l'API open data de Bordeaux. Pour obtenir une [clef](https://data.bordeaux-metropole.fr/opendata/key))
5252
- MISTRAL_API_KEY="YOUR KEY HERE" (pour l'utilisation de l'API Mistral. Pour obtenir une [clef](https://mistral.ai/))
53+
- PORTKEY_API_KEY="YOUR KEY HERE" (pour l'utilisation de l'API Portkey. Pour obtenir une [clef](https://app.portkey.ai/))
54+
- PORTKEY_VIRTUAL_KEY="YOUR KEY HERE" (endpoint configuré sur-mesure dans Portkey . Pour obtenir une [clef](https://app.portkey.ai/))
5355

5456
## Études :
5557

@@ -68,3 +70,14 @@ Identique à l'image précédente, mais en 3D afin de mieux observer certains ph
6870
![image](https://user-images.githubusercontent.com/8374843/96337330-a2ec7680-1086-11eb-84ec-c42c4cd5f7f6.png)
6971

7072
Détection d'anomalies sur la station `Rue de la Croix Blanche` à partir des données en temps réel de la station.
73+
74+
75+
### LLM :
76+
77+
- Monitoring via [Portkey AI](https://app.portkey.ai/) (Open Source : https://github.com/Portkey-AI/gateway)
78+
- Utilisation de l'API Mistral.
79+
- Exemple de use-cases :
80+
- Poser des questions sur le statut / nombre de vélos disponibles sur une station (données internes via open data).
81+
- Connaitre les stations les plus proches d'une adresse.
82+
- Calculer la distance entre deux stations et le temps pour s'y rendre.
83+
- Prédiction sur le nombre de vélos / places disponibles sur une station sur un horizon défini par l'utilisateur (inférieur à plus d'un jour).

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ dependencies = [
3030
"langchain-experimental==0.3.4",
3131
"langchain-mistralai==0.2.9",
3232
"tabulate==0.9.0",
33-
"geopy==2.4.1"
33+
"geopy==2.4.1",
34+
"portkey-ai==1.11.1",
35+
"langchain-openai==0.3.9"
3436
]
3537

3638
[project.optional-dependencies]

src/vcub_keeper/llm/agent.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from langchain_core.rate_limiters import InMemoryRateLimiter
99
from langchain_experimental.agents import create_pandas_dataframe_agent
1010
from langchain_mistralai.chat_models import ChatMistralAI
11+
from langchain_openai import ChatOpenAI
12+
from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders
1113

1214
from vcub_keeper.config import CONFIG_LLM
1315
from vcub_keeper.llm.crewai.tool_python import (
@@ -21,10 +23,11 @@
2123
load_dotenv()
2224

2325

24-
MISTRAL_API_KEY = os.getenv("MISTRAL_API_KEY")
26+
PORTKEY_API_KEY = os.getenv("PORTKEY_API_KEY")
27+
PORTKEY_VIRTUAL_KEY = os.getenv("PORTKEY_VIRTUAL_KEY")
2528

2629

27-
def create_chat(model: str, temperature: float = 0.1) -> ChatMistralAI:
30+
def create_chat(model: str, temperature: float = 0.1, agent_name_monitor: str = "chat_vcub_keeper") -> ChatMistralAI:
2831
"""
2932
3033
@@ -35,6 +38,8 @@ def create_chat(model: str, temperature: float = 0.1) -> ChatMistralAI:
3538
_description_
3639
temperature : float, optional
3740
_description_, by default 0.0
41+
agent_name_monitor : str, optional
42+
Nom du user pour le monitoring via portkey, by default "chat_vcub_keeper"
3843
3944
Returns
4045
-------
@@ -48,22 +53,22 @@ def create_chat(model: str, temperature: float = 0.1) -> ChatMistralAI:
4853
# To avoid rate limit errors (429 - Requests rate limit exceeded)
4954
rate_limiter = InMemoryRateLimiter(requests_per_second=3, check_every_n_seconds=0.3, max_bucket_size=4)
5055

51-
# Initialize memory for conversation history
52-
# memory = ConversationBufferMemory(memory_key="chat_history")
56+
portkey_headers = createHeaders(
57+
api_key=PORTKEY_API_KEY,
58+
virtual_key=PORTKEY_VIRTUAL_KEY,
59+
provider="mistral",
60+
metadata={"_user": agent_name_monitor},
61+
)
5362

54-
chat_llm = ChatMistralAI(
63+
chat_llm = ChatOpenAI(
64+
api_key="X",
65+
base_url=PORTKEY_GATEWAY_URL,
66+
rate_limiter=rate_limiter,
67+
default_headers=portkey_headers,
5568
model=model,
5669
temperature=temperature,
57-
api_key=MISTRAL_API_KEY,
58-
rate_limiter=rate_limiter,
59-
# memory=memory,
60-
random_seed=42,
70+
seed=42,
6171
verbose=True,
62-
# model_kwargs={
63-
# "top_p": 0.92,
64-
# "repetition_penalty": 1.1,
65-
# "max_tokens": 1024, # Limit token generation
66-
# },
6772
)
6873

6974
return chat_llm

tests/llm/test_base_llm.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ def test_distance_calculation(agent, capfd):
163163
or "1 minute" in response["output"].lower()
164164
or "1.59" in response["output"].lower()
165165
or "1,59" in response["output"].lower()
166-
or " 160 secondes" in response["output"].lower()
166+
or "160 secondes" in response["output"].lower()
167+
or "2 minutes et 40 secondes" in response["output"].lower()
167168
)
168169
assert "meriadeck" in response["output"].lower()
169170
assert "place gambetta" in response["output"].lower()

0 commit comments

Comments
 (0)