mirror of
https://github.com/neuml/txtai.git
synced 2026-10-11 21:39:55 +00:00
28 KiB
28 KiB
In [ ]:
%%capture
!pip install git+https://github.com/neuml/txtai#egg=txtai[graph] datasetsIn [34]:
from datasets import load_dataset
import txtai
# Load dataset
ds = load_dataset("fka/awesome-chatgpt-prompts", split="train")
def stream():
for row in ds:
yield f"{row['act']} {row['prompt']}"
# Build sparse keyword index
embeddings = txtai.Embeddings(keyword=True, content=True)
embeddings.index(stream())
embeddings.search("Linux terminal", 1)Out [34]:
[{'id': '0',
'text': 'Linux Terminal I want you to act as a linux terminal. I will type commands and you will reply with what the terminal should show. I want you to only reply with the terminal output inside one unique code block, and nothing else. do not write explanations. do not type commands unless I instruct you to do so. when i need to tell you something in english, i will do so by putting text inside curly brackets {like this}. my first command is pwd',
'score': 0.5932681465337526}]In [19]:
# Build hybrid index
embeddings = txtai.Embeddings(hybrid=True, content=True)
embeddings.index(stream())
embeddings.search("Linux terminal", 1)Out [19]:
[{'id': '0',
'text': 'Linux Terminal I want you to act as a linux terminal. I will type commands and you will reply with what the terminal should show. I want you to only reply with the terminal output inside one unique code block, and nothing else. do not write explanations. do not type commands unless I instruct you to do so. when i need to tell you something in english, i will do so by putting text inside curly brackets {like this}. my first command is pwd',
'score': 0.6078515601252442}]In [20]:
embeddings.search("Linux terminal", 1, weights=1)Out [20]:
[{'id': '0',
'text': 'Linux Terminal I want you to act as a linux terminal. I will type commands and you will reply with what the terminal should show. I want you to only reply with the terminal output inside one unique code block, and nothing else. do not write explanations. do not type commands unless I instruct you to do so. when i need to tell you something in english, i will do so by putting text inside curly brackets {like this}. my first command is pwd',
'score': 0.6224349737167358}]In [21]:
embeddings.search("Linux terminal", 1, weights=0)Out [21]:
[{'id': '0',
'text': 'Linux Terminal I want you to act as a linux terminal. I will type commands and you will reply with what the terminal should show. I want you to only reply with the terminal output inside one unique code block, and nothing else. do not write explanations. do not type commands unless I instruct you to do so. when i need to tell you something in english, i will do so by putting text inside curly brackets {like this}. my first command is pwd',
'score': 0.5932681465337526}]In [22]:
# Build index with subindexes
embeddings = txtai.Embeddings(
content=True,
defaults=False,
indexes={
"sparse": {
"keyword": True
},
"dense":{
}
}
)
embeddings.index(stream())
# Run search
embeddings.search("select id, text, score from txtai where similar('Linux terminal', 'sparse') and similar('Linux terminal', 'dense')", 1)Out [22]:
[{'id': '0',
'text': 'Linux Terminal I want you to act as a linux terminal. I will type commands and you will reply with what the terminal should show. I want you to only reply with the terminal output inside one unique code block, and nothing else. do not write explanations. do not type commands unless I instruct you to do so. when i need to tell you something in english, i will do so by putting text inside curly brackets {like this}. my first command is pwd',
'score': 0.6078515601252442}]In [23]:
embeddings.search("select id, text, score from txtai where similar('Linux terminal', 'dense')", 1)Out [23]:
[{'id': '0',
'text': 'Linux Terminal I want you to act as a linux terminal. I will type commands and you will reply with what the terminal should show. I want you to only reply with the terminal output inside one unique code block, and nothing else. do not write explanations. do not type commands unless I instruct you to do so. when i need to tell you something in english, i will do so by putting text inside curly brackets {like this}. my first command is pwd',
'score': 0.6224349737167358}]In [24]:
embeddings.search("select id, text, score from txtai where similar('Linux terminal', 'sparse')", 1)Out [24]:
[{'id': '0',
'text': 'Linux Terminal I want you to act as a linux terminal. I will type commands and you will reply with what the terminal should show. I want you to only reply with the terminal output inside one unique code block, and nothing else. do not write explanations. do not type commands unless I instruct you to do so. when i need to tell you something in english, i will do so by putting text inside curly brackets {like this}. my first command is pwd',
'score': 0.5932681465337526}]In [25]:
# Build index with graph subindex
embeddings = txtai.Embeddings(
content=True,
defaults=False,
functions=[
{"name": "graph", "function": "indexes.act.graph.attribute"}
],
expressions=[
{"name": "topic", "expression": "graph(indexid, 'topic')"},
],
indexes={
"act": {
"keyword": True,
"columns": {
"text": "act"
},
"graph": {
"topics": {}
}
},
"prompt":{
"columns": {
"text": "prompt"
}
}
}
)
embeddings.index(ds)
# Run search
embeddings.search("select id, act, prompt, score, topic from txtai where similar('Linux terminal')", 1)Out [25]:
[{'id': '0',
'act': 'Linux Terminal',
'prompt': 'I want you to act as a linux terminal. I will type commands and you will reply with what the terminal should show. I want you to only reply with the terminal output inside one unique code block, and nothing else. do not write explanations. do not type commands unless I instruct you to do so. when i need to tell you something in english, i will do so by putting text inside curly brackets {like this}. my first command is pwd',
'score': 0.6382951796072414,
'topic': 'terminal_linux_sql'}]In [26]:
# Build index with graph subindex
embeddings = txtai.Embeddings(
content=True,
defaults=False,
functions=[
{"name": "graph", "function": "indexes.act.graph.attribute"}
],
expressions=[
{"name": "topic", "expression": "graph(indexid, 'topic')"},
],
indexes={
"act": {
"path": "intfloat/e5-small-v2",
"columns": {
"text": "act"
},
"graph": {
"topics": {}
}
},
"prompt":{
"path": "sentence-transformers/all-MiniLM-L6-v2",
"columns": {
"text": "prompt"
}
}
}
)
embeddings.index(ds)
# Run search
embeddings.search("select id, act, prompt, score, topic from txtai where similar('Linux terminal')", 1)Out [26]:
[{'id': '0',
'act': 'Linux Terminal',
'prompt': 'I want you to act as a linux terminal. I will type commands and you will reply with what the terminal should show. I want you to only reply with the terminal output inside one unique code block, and nothing else. do not write explanations. do not type commands unless I instruct you to do so. when i need to tell you something in english, i will do so by putting text inside curly brackets {like this}. my first command is pwd',
'score': 1.0,
'topic': 'linux_terminal'}]In [27]:
embeddings.search("select id, act, prompt, score from txtai where similar('Linux terminal', 'act') and similar('Linux terminal', 'prompt')", 1)Out [27]:
[{'id': '0',
'act': 'Linux Terminal',
'prompt': 'I want you to act as a linux terminal. I will type commands and you will reply with what the terminal should show. I want you to only reply with the terminal output inside one unique code block, and nothing else. do not write explanations. do not type commands unless I instruct you to do so. when i need to tell you something in english, i will do so by putting text inside curly brackets {like this}. my first command is pwd',
'score': 0.7881423830986023}]In [28]:
# Build index with graph subindex
app = txtai.Application("""
writable: True
embeddings:
content: True
defaults: False
functions:
- name: graph
function: indexes.act.graph.attribute
expressions:
- name: topic
expression: graph(indexid, 'topic')
indexes:
act:
path: intfloat/e5-small-v2
columns:
text: act
graph:
topics:
prompt:
path: sentence-transformers/all-MiniLM-L6-v2
columns:
text: prompt
""")
app.add(ds)
app.index()
app.search("select id, act, prompt, topic, score from txtai where similar('Linux terminal', 'act') and similar('Linux terminal', 'prompt')", 1)Out [28]:
[{'id': '0',
'act': 'Linux Terminal',
'prompt': 'I want you to act as a linux terminal. I will type commands and you will reply with what the terminal should show. I want you to only reply with the terminal output inside one unique code block, and nothing else. do not write explanations. do not type commands unless I instruct you to do so. when i need to tell you something in english, i will do so by putting text inside curly brackets {like this}. my first command is pwd',
'topic': 'linux_terminal',
'score': 0.7881423830986023}]In [29]:
# Top-level import includes Application and Embeddings
import txtai
app = txtai.Application("""writable: False""")
embeddings = txtai.Embeddings()In [30]:
# Ids are automatically generated when omitted
embeddings.index(["test"])
print(embeddings.search("test"))
# UUID ids are also supported - use any of the methods in https://docs.python.org/3/library/uuid.html
embeddings = txtai.Embeddings(autoid="uuid5")
embeddings.index(["test"])
embeddings.search("test")Out [30]:
[(0, 0.9999998807907104)]
[('4be0643f-1d98-573b-97cd-ca98a65347dd', 0.9999998807907104)]In [31]:
import torch
from txtai import LLM
# Create model and set dtype to use 16-bit floats
llm = LLM("tiiuae/falcon-rw-1b", torch_dtype=torch.bfloat16)In [32]:
print(llm("Write a short list of things to do in Paris", maxlength=55)). - Visit the Eiffel Tower. - Visit the Louvre. - Visit the Arc de Triomphe. - Visit the Notre Dame Cathedral. - Visit the Sacre Coeur Basilica