test tse

import gradio as gr
from langchain.chat_models import init_chat_model
from langchain_core.tools import tool
from langchain_core.messages import HumanMessage, AIMessage
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder

# ----------------------
# 1. Tool
# ----------------------
@tool
def smart_get_info(app: str) -> str:
   """Fournit des informations sur une application."""
   return f"[INFO] L'application {app} est gérée par l'équipe IT Sécurité."

tools = [smart_get_info]

# ----------------------
# 2. Modèle LLM Mistral via Ollama
# ----------------------
from langchain_openai import ChatOpenAI


llm = ChatOpenAI(
   base_url="http://localhost:11434/v1",
   model_name="mistral:latest",
   api_key="ollama",
   temperature=0,
   streaming=True
)

from langchain.chat_models import ChatOpenAI

llm = ChatOpenAI(
   base_url="https://mon-vllm-serveur:8000/v1",  # HTTPS
   api_key="sk-mon-token",
   model_name="mistral",
   request_timeout=60,
   default_headers={"Authorization": "Bearer sk-mon-token"},
   http_client_args={
       "verify": "/chemin/vers/ca.pem"  # ← ici ton fichier CA
   }
)

 

# ----------------------
# 3. Prompt Chat (complet)
# ----------------------
prompt = ChatPromptTemplate.from_messages([
   ("system", """Tu es un assistant spécialisé en recertification d'applications.
Réponds normalement aux questions simples comme 'Bonjour', sans appeler d'outil.
N'utilise les tools que si c'est nécessaire pour répondre à une demande d'information précise sur une application."""),
   MessagesPlaceholder(variable_name="chat_history"),
   ("human", "{input}"),
   MessagesPlaceholder(variable_name="agent_scratchpad"),
])

# ----------------------
# 4. Création de l’agent
# ----------------------
agent = create_tool_calling_agent(llm=llm, tools=tools, prompt=prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

# ----------------------
# 5. Interface Gradio avec gestion de chat_history
# ----------------------
chat_history = []

def ask_agent(user_input):
   global chat_history
   chat_history.append(HumanMessage(content=user_input))
   response = agent_executor.invoke({
       "input": user_input,
       "chat_history": chat_history
   })
   chat_history.append(AIMessage(content=response["output"]))  # ← on reconstruit un message ici
   return response["output"]  # on retourne directement le texte


gr.Interface(
   fn=ask_agent,
   inputs="text",
   outputs="text",
   title="Agent Mistral (Ollama) - Tool Calling Moderne",
   live=False
).launch(server_name="127.0.0.1", share=False)
 

About Author

Directeur de Publication