mirror of
https://github.com/neuml/txtai.git
synced 2026-10-12 02:19:52 +00:00
13 KiB
13 KiB
In [ ]:
%%capture
!pip install git+https://github.com/neuml/txtai#egg=txtai[api]In [ ]:
from txtai import LLM, Workflow
from txtai.workflow import TemplateTask
# Create LLM
llm = LLM("Qwen/Qwen3-4B-Instruct-2507")
# Define workflow or chaining of tasks together.
workflow = Workflow([
TemplateTask(
template="Translate text '{statement}' to {language} if the text is English, otherwise keep the original text",
action=llm
),
TemplateTask(
template="What language is the following text. Only print the answer? {text}",
action=llm
)
])
inputs = [
{"statement": "Hello, how are you", "language": "French"},
{"statement": "Hallo, wie geht's dir", "language": "French"}
]
print(list(workflow(inputs)))['French', 'German']
In [2]:
%%writefile workflow.yml
llm:
path: Qwen/Qwen3-4B-Instruct-2507
workflow:
chain:
tasks:
- task: template
template: Translate text '{statement}' to {language} if the text is English, otherwise keep the original text
action: llm
- task: template
template: What language is the following text. Only print the answer? {text}
action: llmWriting workflow.yml
In [2]:
from txtai import Application
app = Application("workflow.yml")
print(list(app.workflow("chain", inputs)))['French', 'German']
In [ ]:
# Start an API service
!CONFIG=workflow.yml nohup uvicorn "txtai.api:app" &> api.log &
!sleep 60In [3]:
import requests
# Run API request
requests.post("http://localhost:8000/workflow", json={"name": "chain", "elements": inputs}).json()Out [3]:
['French', 'German']
In [4]:
%%bash
curl -s -X POST "http://localhost:8000/workflow" \
-H "Content-Type: application/json" \
--data @- << EOF
{
"name": "chain",
"elements": [
{"statement": "Hello, how are you", "language": "French"},
{"statement": "Hallo, wie geht's dir", "language": "French"}
]
}
EOF["French","German"]
In [10]:
%%writefile search.yml
writable: false
cloud:
provider: huggingface-hub
container: neuml/txtai-intro
rag:
path: Qwen/Qwen3-4B-Instruct-2507
output: reference
template: |
Answer the following question using only the context below.
Question: {question}
Context: {context}
workflow:
search:
tasks:
- action: rag
- task: template
template: "{answer}\n\nReference: {reference}"
rules:
answer: I don't have data on thatWriting search.yml
In [ ]:
app = Application("search.yml")
print(list(app.workflow("search", ["Find something about North America"])))["Canada's last fully intact ice shelf has suddenly collapsed, forming a Manhattan-sized iceberg.\n\nReference: 1"]