agent
Conversational AI agent for Vietnamese language tasks using OpenAI or Azure OpenAI.
Usage
from underthesea import agent
response = agent("Xin chào, NLP là gì?")
print(response)
# NLP (Natural Language Processing) là xử lý ngôn ngữ tự nhiên...
Installation
pip install "underthesea[agent]"
Configuration
Environment Variables
- OpenAI
- Azure OpenAI
export OPENAI_API_KEY="sk-..."
export OPENAI_MODEL="gpt-4o-mini" # optional
export AZURE_OPENAI_API_KEY="..."
export AZURE_OPENAI_ENDPOINT="https://xxx.openai.azure.com"
export AZURE_OPENAI_DEPLOYMENT="my-gpt4-deployment" # optional
export AZURE_OPENAI_API_VERSION="2024-02-01" # optional
Environment Variables Reference
| Provider | Variable | Required | Description |
|---|---|---|---|
| OpenAI | OPENAI_API_KEY | Yes | OpenAI API key |
| OpenAI | OPENAI_MODEL | No | Model name (default: gpt-4o-mini) |
| Azure | AZURE_OPENAI_API_KEY | Yes | Azure OpenAI API key |
| Azure | AZURE_OPENAI_ENDPOINT | Yes | Azure OpenAI endpoint URL |
| Azure | AZURE_OPENAI_DEPLOYMENT | No | Deployment name (default: gpt-4o-mini) |
| Azure | AZURE_OPENAI_API_VERSION | No | API version (default: 2024-02-01) |
Function Signature
def agent(
message: str,
model: str | None = None,
system_prompt: str | None = None,
provider: str | None = None,
api_key: str | None = None,
azure_endpoint: str | None = None,
azure_api_version: str | None = None,
) -> str
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
message | str | User message to send | |
model | str | None | Model/deployment name. Falls back to env var or gpt-4o-mini |
system_prompt | str | None | Custom system prompt |
provider | str | None | Provider: "openai" or "azure". Auto-detected if not specified |
api_key | str | None | API key. Falls back to environment variable |
azure_endpoint | str | None | Azure endpoint URL. Falls back to AZURE_OPENAI_ENDPOINT |
azure_api_version | str | None | Azure API version. Falls back to env var or 2024-02-01 |
Returns
| Type | Description |
|---|---|
str | Assistant response |
Methods
agent.reset()
Clear conversation history.
agent.reset()
agent.history
Get conversation history (read-only copy).
history = agent.history
# [{'role': 'user', 'content': '...'}, {'role': 'assistant', 'content': '...'}]
Examples
Basic Conversation
from underthesea import agent
# First message
response = agent("Xin chào!")
print(response)
# Xin chào! Tôi có thể giúp gì cho bạn?
# Follow-up (history is maintained)
response = agent("NLP là gì?")
print(response)
# NLP (Natural Language Processing) là...
# Check history
print(len(agent.history)) # 4 (2 user + 2 assistant messages)
# Reset conversation
agent.reset()
print(len(agent.history)) # 0
Custom System Prompt
from underthesea import agent
response = agent(
"Chào bạn",
system_prompt="Bạn là trợ lý chuyên về ẩm thực Việt Nam."
)
Custom Model
from underthesea import agent
# Use GPT-4
response = agent("Giải thích machine learning", model="gpt-4")
# Use GPT-4 Turbo
response = agent("Giải thích deep learning", model="gpt-4-turbo")
Azure OpenAI
from underthesea import agent
# Using environment variables (recommended)
# export AZURE_OPENAI_API_KEY="..."
# export AZURE_OPENAI_ENDPOINT="https://xxx.openai.azure.com"
# export AZURE_OPENAI_DEPLOYMENT="my-gpt4"
response = agent("Xin chào")
# Or explicit configuration
response = agent(
"Xin chào",
provider="azure",
api_key="your-azure-key",
azure_endpoint="https://your-resource.openai.azure.com",
model="your-deployment-name"
)
Vietnamese NLP Assistant
from underthesea import agent, word_tokenize, pos_tag
# Use agent to explain NLP concepts
text = "Học máy là gì?"
response = agent(text)
print(response)
# Combine with other underthesea functions
text = "Việt Nam là quốc gia xinh đẹp"
tokens = word_tokenize(text)
tags = pos_tag(text)
# Ask agent to explain the results
response = agent(f"Giải thích kết quả POS tagging: {tags}")
print(response)
LLM Class
For more control, use the LLM class directly:
from underthesea.agent import LLM
# Initialize
llm = LLM(model="gpt-4")
# Chat with custom messages
messages = [
{"role": "system", "content": "You are a Vietnamese language expert."},
{"role": "user", "content": "Explain word segmentation in Vietnamese."}
]
response = llm.chat(messages)
print(response)
# Check provider
print(llm.provider) # 'openai' or 'azure'
print(llm.model) # 'gpt-4'
Agent Class with Tools
Create custom agents with function calling support using OpenAI's tools API.
Basic Usage
from underthesea.agent import Agent, Tool
# Define a tool as a Python function
def get_weather(location: str) -> dict:
"""Get current weather for a location."""
return {"location": location, "temp": 25, "condition": "sunny"}
# Create agent with tools
my_agent = Agent(
name="weather_assistant",
tools=[Tool(get_weather, description="Get weather for a city")],
instruction="You are a helpful weather assistant."
)
# Use the agent - it will automatically call tools when needed
response = my_agent("What's the weather in Hanoi?")
print(response)
# The weather in Hanoi is 25°C and sunny.
# Reset conversation
my_agent.reset()
Agent Constructor
Agent(
name: str,
tools: list[Tool] | None = None,
instruction: str | None = None,
max_iterations: int = 10,
)
| Parameter | Type | Default | Description |
|---|---|---|---|
name | str | Agent name | |
tools | list[Tool] | None | List of tools available to the agent |
instruction | str | "You are a helpful assistant." | System instruction |
max_iterations | int | 10 | Maximum tool calling iterations |
Tool Class
Wrap Python functions as agent tools:
from underthesea.agent import Tool
def search(query: str, limit: int = 10) -> list:
"""Search for items matching the query."""
return [{"title": f"Result for {query}"}]
tool = Tool(
func=search,
name="web_search", # Optional, defaults to function name
description="Search the web" # Optional, defaults to docstring
)
# Convert to OpenAI format
openai_format = tool.to_openai_tool()
# Execute directly
result = tool(query="python", limit=5)
Tool Constructor
Tool(
func: Callable,
name: str | None = None,
description: str | None = None,
)
| Parameter | Type | Default | Description |
|---|---|---|---|
func | Callable | The function to wrap | |
name | str | None | Tool name (defaults to function name) |
description | str | None | Tool description (defaults to docstring) |