Skip to main content

Agente langchain con LLM

This project has been archived.

The maintainers of this project have marked this project as archived. No new releases are expected.

Project description

Sonika LangChain Bot PyPI Downloads

Una librería Python que implementa un bot conversacional utilizando LangChain con capacidades BDI (Belief-Desire-Intention) y clasificación de texto.

Instalación

pip install sonika-langchain-bot

Requisitos previos

Necesitarás las siguientes API keys:

  • OpenAI API Key

Crea un archivo .env en la raíz de tu proyecto con las siguientes variables:

OPENAI_API_KEY=tu_api_key_aqui

Características principales

  • Bot conversacional con arquitectura BDI
  • Clasificación de texto
  • Ejecución de código personalizado por medio de tools

Uso básico

Ejemplo de Bot BDI

from sonika_langchain_bot.langchain_bdi import Belief, BeliefType
from sonika_langchain_bot.langchain_bot_agent_bdi import LangChainBot
from sonika_langchain_bot.langchain_models import OpenAILanguageModel
from langchain_openai import OpenAIEmbeddings

# Inicializar el modelo de lenguaje
language_model = OpenAILanguageModel(api_key, model_name='gpt-4o-mini', temperature=1)
embeddings = OpenAIEmbeddings(api_key=api_key)

# Configurar herramientas propias o de terceros
search = TavilySearchResults(max_results=2, api_key=api_key_tavily)
tools = [search]

# Configurar creencias
beliefs = [
    Belief(
        content="Eres un asistente de chat",
        type=BeliefType.PERSONALITY,
        confidence=1,
        source='personality'
    )
]

# Crear instancia del bot
bot = LangChainBot(language_model, embeddings, beliefs=beliefs, tools=tools)

# Obtener respuesta
response = bot.get_response("Hola como te llamas?")

bot = LangChainBot(language_model, embeddings, beliefs=beliefs, tools=tools)

user_message = 'Hola como me llamo?'

#Cargas la conversacion previa con el bot
bot.load_conversation_history([Message(content="Mi nombre es Erley", is_bot=False)])
# Obtener la respuesta del bot
response_model: ResponseModel = bot.get_response(user_message)
bot_response = response_model

print(bot_response)

#o por streaming
for chunk in bot.get_response_stream(user_message):
    print(chunk)

Ejemplo de Clasificación de Texto

from sonika_langchain_bot.langchain_clasificator import TextClassifier
from sonika_langchain_bot.langchain_models import OpenAILanguageModel
from pydantic import BaseModel, Field

# Definir estructura de clasificación
class Classification(BaseModel):
    intention: str = Field()
    sentiment: str = Field(..., enum=["feliz", "neutral", "triste", "excitado"])
    aggressiveness: int = Field(
        ...,
        description="describes how aggressive the statement is",
        enum=[1, 2, 3, 4, 5],
    )
    language: str = Field(
        ..., enum=["español", "ingles", "frances", "aleman", "italiano"]
    )

# Inicializar clasificador
model = OpenAILanguageModel(api_key=api_key)
classifier = TextClassifier(llm=model, validation_class=Classification)

# Clasificar texto
result = classifier.classify("Tu texto aquí")

Contribución

Las contribuciones son bienvenidas. Por favor, abre un issue para discutir los cambios importantes que te gustaría hacer.

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

sonika_langchain_bot-0.0.9.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sonika_langchain_bot-0.0.9-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file sonika_langchain_bot-0.0.9.tar.gz.

File metadata

  • Download URL: sonika_langchain_bot-0.0.9.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.2

File hashes

Hashes for sonika_langchain_bot-0.0.9.tar.gz
Algorithm Hash digest
SHA256 629eb1c97fa48bf8d6d2d7af78164340d3d2cd7526804b6361ea305369f24c29
MD5 009b9e86f677e92b4e521f053e7be0c3
BLAKE2b-256 13ec5a323b0931de560f01c2d3207b207df899b16473f3137fe98058044ad722

See more details on using hashes here.

File details

Details for the file sonika_langchain_bot-0.0.9-py3-none-any.whl.

File metadata

File hashes

Hashes for sonika_langchain_bot-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 864f83025517b16669914eb908f664e476d9ef5b6c7266c993f4c218a0e984a8
MD5 0c01cb41b140b0abc8805f74274736ec
BLAKE2b-256 b105d0ab3fda17c85b188effe488bd45f12172ee87e58c29ea3e232a48a04f38

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page