varunshenoy / super-json-mode Goto Github PK
View Code? Open in Web Editor NEWLow latency JSON generation using LLMs ⚡️
Low latency JSON generation using LLMs ⚡️
Hi Varun, Great work on the repo so far.
I am trying to integrate the super json module to generate neatly formatted jsons for RAG evaluation using custom LLMs
The Mistral 7B example mentioned in README is not working in my machine. Can you please have a look? Also, import json was missing.
from transformers import AutoTokenizer, AutoModelForCausalLM
from superjsonmode.integrations.transformers import StructuredOutputForModel
from pydantic import BaseModel
import json
device = "cuda"
model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-Instruct-v0.2").to(device)
tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.2")
# Create a structured output object
structured_model = StructuredOutputForModel(model, tokenizer)
passage = """..."""
class QuarterlyReport(BaseModel):
company: str
stock_ticker: str
date: str
reported_revenue: str
dividend: str
prompt_template = """[INST]{prompt}
Based on this excerpt, extract the correct value for "{key}". Keep it succinct. It should have a type of `{type}`.[/INST]
{key}: """
output = structured_model.generate(passage,
extraction_prompt_template=prompt_template,
schema=QuarterlyReport,
batch_size=6)
print(json.dumps(output, indent=2))
# {
# "company": "NVIDIA",
# "stock_ticker": "NVDA",
# "date": "2023-10",
# "reported_revenue": "18.12 billion dollars",
# "dividend": "0.04"
# }
Loading checkpoint shards: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████| 3/3 [00:01<00:00, 2.38it/s]
Traceback (most recent call last):
File "/home/deval/Documents/Work/Deval/ES/LlamaSearch/src/temp1.py", line 29, in <module>
output = structured_model.generate(passage,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/deval/Document/Work/miniconda3/envs/es/lib/python3.11/site-packages/superjsonmode/integrations/transformers.py", line 45, in generate
embeds = self.tokenizer(prompts, return_tensors="pt", padding=True).to(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/deval/Document/Work/miniconda3/envs/es/lib/python3.11/site-packages/transformers/tokenization_utils_base.py", line 2803, in __call__
encodings = self._call_one(text=text, text_pair=text_pair, **all_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/deval/Document/Work/miniconda3/envs/es/lib/python3.11/site-packages/transformers/tokenization_utils_base.py", line 2889, in _call_one
return self.batch_encode_plus(
^^^^^^^^^^^^^^^^^^^^^^^
File "/home/deval/Document/Work/miniconda3/envs/es/lib/python3.11/site-packages/transformers/tokenization_utils_base.py", line 3071, in batch_encode_plus
padding_strategy, truncation_strategy, max_length, kwargs = self._get_padding_truncation_strategies(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/deval/Document/Work/miniconda3/envs/es/lib/python3.11/site-packages/transformers/tokenization_utils_base.py", line 2708, in _get_padding_truncation_strategies
raise ValueError(
ValueError: Asking to pad but the tokenizer does not have a padding token. Please select a token to use as `pad_token` `(tokenizer.pad_token = tokenizer.eos_token e.g.)` or add a new pad token via `tokenizer.add_special_tokens({'pad_token': '[PAD]'})`.
Nice project!
I believe this project can greatly benefit from https://github.com/sgl-project/sglang. You can try to use SGLang as a backend for local models.
maybe this is a dumb question, but if you decompose the schema into K queries to extract values in parallel, don't you have to repeat the text input K times? Or do you have a way around this?
We have an application where users supply the JSON schema for use directly. I saw that you convert the Pydantic model to JSON internally. Any thoughts on supporting JSON schema directly instead of JSON models or suggestions for converting json schema strings into the pydantic models?
Very cool work by the way. Thanks for this effort!
Dear @varunshenoy,
I modified code to incorporate Ollama into the SuperJSON. However, It is unable to do batch processing. If you think it would be considerable placeholder until Ollama adds batch processing I can make a PR.
Best,
Naman
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.