mirror of
https://github.com/neuml/txtai.git
synced 2026-10-12 02:19:52 +00:00
12 KiB
12 KiB
In [ ]:
%%capture
!pip install git+https://github.com/neuml/txtai#egg=txtai autoawq outlinesIn [ ]:
from txtai import LLM
llm = LLM("TheBloke/Mistral-7B-OpenOrca-AWQ")
def rag(question, text):
prompt = f"""<|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: {text} <|im_end|>
<|im_start|>assistant
"""
return llm(prompt, maxlength=4096)
In [ ]:
# Manually generated context. Replace with an Embedding search or other request. See prior examples on txtai's documentation site for more.
context = """
England's terrain chiefly consists of low hills and plains, especially in the centre and south.
The Battle of Hastings was fought on 14 October 1066 between the Norman army of William, the Duke of Normandy, and an English army under the Anglo-Saxon King Harold Godwinson
Bounded by the Atlantic Ocean on the east, Brazil has a coastline of 7,491 kilometers (4,655 mi).
Spain pioneered the exploration of the New World and the first circumnavigation of the globe.
Christopher Columbus lands in the Caribbean in 1492.
"""
print(rag("List the countries discussed", context))1. England 2. Brazil 3. Spain
In [ ]:
from typing import List
from outlines.integrations.transformers import JSONPrefixAllowedTokens
from pydantic import BaseModel
class Response(BaseModel):
answers: List[str]
citations: List[str]
# Define method that guides LLM generation
prefix_allowed_tokens_fn=JSONPrefixAllowedTokens(
schema=Response,
tokenizer_or_pipe=llm.generator.llm.pipeline.tokenizer,
whitespace_pattern=r" ?"
)
def rag(question, text):
prompt = f"""<|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: {text} <|im_end|>
<|im_start|>assistant
"""
return llm(prompt, maxlength=4096, prefix_allowed_tokens_fn=prefix_allowed_tokens_fn)
In [ ]:
import json
json.loads(rag("List the countries discussed", context)){'answers': ['England', 'Brazil', 'Spain'],
'citations': ["England's terrain chiefly consists of low hills and plains, especially in the centre and south.",
'The Battle of Hastings was fought on 14 October 1066 between the Norman army of William, the Duke of Normandy, and an English army under the Anglo-Saxon King Harold Godwinson.',
'Bounded by the Atlantic Ocean on the east, Brazil has a coastline of 7,491 kilometers (4,655 mi).',
'Spain pioneered the exploration of the New World and the first circumnavigation of the globe.',
'Christopher Columbus lands in the Caribbean in 1492.']}In [ ]:
class Response(BaseModel):
countries: List[str]
geography: List[str]
years: List[str]
people: List[str]
prefix_allowed_tokens_fn=JSONPrefixAllowedTokens(
schema=Response,
tokenizer_or_pipe=llm.generator.llm.pipeline.tokenizer,
whitespace_pattern=r" ?"
)
def rag(question, text):
prompt = f"""<|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: {text} <|im_end|>
<|im_start|>assistant
"""
return llm(prompt, maxlength=4096, prefix_allowed_tokens_fn=prefix_allowed_tokens_fn)
In [ ]:
json.loads(rag("List the entities discussed", context)){'countries': ['England', 'Brazil', 'Spain'],
'geography': ['low hills and plains', 'Atlantic Ocean', 'New World'],
'years': ['1066', '1492'],
'people': ['William, the Duke of Normandy',
'Anglo-Saxon King Harold Godwinson',
'Christopher Columbus']}