mirror of
https://github.com/neuml/txtai.git
synced 2026-10-11 21:39:55 +00:00
20 KiB
20 KiB
In [1]:
%%capture
!pip install git+https://github.com/neuml/txtai#egg=txtai[api,pipeline]In [ ]:
from txtai import Embeddings, LLM
# Load Wikipedia Embeddings database
embeddings = Embeddings()
embeddings.load(provider="huggingface-hub", container="neuml/txtai-wikipedia")
# Create LLM
llm = LLM("Qwen/Qwen3-4B-Instruct-2507")In [3]:
# Prompt template
prompt = """<|im_start|>system
You are a friendly assistant. You answer questions from users.<|im_end|>
<|im_start|>user
Answer the following question using only the context below. Only include information
specifically discussed.
question: {question}
context: {context} <|im_end|>
<|im_start|>assistant
"""In [4]:
question = "How do you make beer?"
# Generate context
context = "\n".join([x["text"] for x in embeddings.search(question)])
print(context)Brewing is the production of beer by steeping a starch source (commonly cereal grains, the most popular of which is barley) in water and fermenting the resulting sweet liquid with yeast. It may be done in a brewery by a commercial brewer, at home by a homebrewer, or communally. Brewing has taken place since around the 6th millennium BC, and archaeological evidence suggests that emerging civilizations, including ancient Egypt, China, and Mesopotamia, brewed beer. Since the nineteenth century the brewing industry has been part of most western economies. Beer is produced through steeping a sugar source (commonly Malted cereal grains) in water and then fermenting with yeast. Brewing has taken place since around the 6th millennium BC, and archeological evidence suggests that this technique was used in ancient Egypt. Descriptions of various beer recipes can be found in Sumerian writings, some of the oldest known writing of any sort. Brewing is done in a brewery by a brewer, and the brewing industry is part of most western economies. In 19th century Britain, technological discoveries and improvements such as Burtonisation and the Burton Union system significantly changed beer brewing. Craft beer is a beer that has been made by craft breweries, which typically produce smaller amounts of beer, than larger "macro" breweries, and are often independently owned. Such breweries are generally perceived and marketed as emphasising enthusiasm, new flavours, and varied brewing techniques.
In [5]:
print(llm(prompt.format(question=question, context=context)))To make beer, you need to steep a starch source, such as malted cereal grains (commonly barley), in water. This process creates a sweet liquid called wort. Then, yeast is added to the wort, which ferments the liquid and produces alcohol and carbon dioxide. The beer is then aged, filtered, and packaged for consumption. This process has been used since around the 6th millennium BC and has been a part of most western economies since the 19th century.
In [6]:
from txtai import RAG
# Create RAG pipeline using existing components. LLM parameter can also be a model path.
rag = RAG(embeddings, llm, template=prompt)In [7]:
print(rag("How do you make wine?", maxlength=2048)["answer"])To make wine, follow these steps: 1. Select the fruit: Choose high-quality grapes or other fruit for wine production. 2. Fermentation: Introduce yeast to the fruit, which will consume the sugar present in the juice and convert it into ethanol and carbon dioxide. 3. Monitor temperature and oxygen levels: Control the temperature and speed of fermentation, as well as the levels of oxygen present in the must at the start of fermentation. 4. Primary fermentation: This stage lasts from 5 to 14 days, during which the yeast consumes the sugar and produces alcohol and carbon dioxide. 5. Secondary fermentation (optional): If desired, allow the wine to undergo a secondary fermentation, which can last another 5 to 10 days. 6. Fermentation location: Choose the appropriate fermentation vessel, such as stainless steel tanks, open wooden vats, wine barrels, or wine bottles for sparkling wines. 7. Bottle and age the wine: Transfer the finished wine into bottles and allow it to age, if desired, to develop flavors and complexity. Remember that wine can be made from various fruits, but grapes are most commonly used, and the term "wine" generally refers to grape wine when used without a qualifier.
In [8]:
%%writefile config.yml
# Load Wikipedia Embeddings index
cloud:
provider: huggingface-hub
container: neuml/txtai-wikipedia
# RAG pipeline configuration
rag:
path: Qwen/Qwen3-4B-Instruct-2507
output: flatten
template: |
<|im_start|>system
You are a friendly assistant. You answer questions from users.<|im_end|>
<|im_start|>user
Answer the following question using only the context below. Only include information
specifically discussed.
question: {question}
context: {context} <|im_end|>
<|im_start|>assistantWriting config.yml
In [7]:
!CONFIG=config.yml nohup uvicorn "txtai.api:app" &> api.log &
!sleep 90In [22]:
!curl "http://localhost:8000/rag?query=how+do+you+make+whisky&maxlength=2048"To make whisky, follow these steps: 1. Choose the grains: Select the grains you want to use for your whisky, such as barley, corn, rye, or wheat. 2. Malt the grains (optional): If using barley, malt the grains by soaking them in water and allowing them to germinate. This process releases enzymes that help break down starches into fermentable sugars. 3. Mill the grains: Grind the grains to create a coarse flour, which will be mixed with water to create a mash. 4. Create the mash: Combine the milled grains with hot water in a large vessel, and let it sit for several hours to allow fermentation to occur. The mash should have a temperature of around 65°C (149°F) to encourage the growth of yeast. 5. Add yeast: Once the mash has cooled to around 30°C (86°F), add yeast to the mixture. The yeast will ferment the sugars in the mash, producing alcohol. 6. Fermentation: Allow the mixture to ferment for several days, during which the yeast will consume the sugars and produce alcohol and carbon dioxide. 7. Distillation: Transfer the fermented liquid, called "wash" to a copper still. Heat the wash in the still, and the alcohol will vaporize and rise through the still's neck. The vapors are then condensed back into a liquid form, creating a high-proof spirit. 8. Maturation: Transfer the distilled spirit to wooden casks, typically made of charred white oak. The spirit will mature in the casks for a specified period, usually ranging from 3 to 25 years. During this time, the wood imparts flavors and color to the whisky. 9. Bottling: Once the whisky has reached the desired maturity, it is bottled and ready for consumption.