|
| 1 | +def test_simple_example() -> None: |
| 2 | + import outlines |
| 3 | + import transformers |
| 4 | + from sieves import Pipeline, tasks, Doc |
| 5 | + |
| 6 | + # Set up model. |
| 7 | + model_name = "HuggingFaceTB/SmolLM2-135M-Instruct" |
| 8 | + model = outlines.models.from_transformers( |
| 9 | + transformers.AutoModelForCausalLM.from_pretrained(model_name), |
| 10 | + transformers.AutoTokenizer.from_pretrained(model_name) |
| 11 | + ) |
| 12 | + |
| 13 | + # Define task. |
| 14 | + task = tasks.Classification(labels=["science", "politics"], model=model) |
| 15 | + |
| 16 | + # Define pipeline with the classification task. |
| 17 | + pipeline = Pipeline(task) |
| 18 | + |
| 19 | + # Define documents to analyze. |
| 20 | + doc = Doc(text="The new telescope captures images of distant galaxies.") |
| 21 | + |
| 22 | + # Run pipeline and print results. |
| 23 | + docs = list(pipeline([doc])) |
| 24 | + # The `results` field contains the structured task output as a unified Pydantic model. |
| 25 | + print(docs[0].results["Classification"]) # ResultMultiLabel(label_scores=[('science', 1.0), ('politics', 0.0)]) |
| 26 | + # The `meta` field contains more information helpful for observability and debugging, such as raw model output and token count information. |
| 27 | + print(docs[0].meta) # {'Classification': { |
| 28 | + # 'raw': ['{ "science": 1.0, "politics": 0 }'], |
| 29 | + # 'usage': {'input_tokens': 2, 'output_tokens': 2, 'chunks': [{'input_tokens': 2, 'output_tokens': 2}]}}, 'usage': {'input_tokens': 2, 'output_tokens': 2} |
| 30 | + # } |
| 31 | + |
| 32 | +def test_advanced_example() -> None: |
| 33 | + import dspy |
| 34 | + import os |
| 35 | + import pydantic |
| 36 | + import chonkie |
| 37 | + import tokenizers |
| 38 | + from sieves import tasks, Doc |
| 39 | + |
| 40 | + # Define which schema of entity to extract. |
| 41 | + class Equation(pydantic.BaseModel, frozen=True): |
| 42 | + id: str = pydantic.Field(description="ID/index of equation in paper.") |
| 43 | + equation: str = pydantic.Field(description="Equation as shown in paper.") |
| 44 | + |
| 45 | + # Setup DSPy model. |
| 46 | + model = dspy.LM( |
| 47 | + "openrouter/google/gemini-3-flash-preview", |
| 48 | + api_base="https://openrouter.ai/api/v1/", |
| 49 | + api_key=os.environ["OPENROUTER_API_KEY"] |
| 50 | + ) |
| 51 | + |
| 52 | + # Build pipeline: ingest -> chunk -> extract. |
| 53 | + pipeline = ( |
| 54 | + tasks.Ingestion() + |
| 55 | + tasks.Chunking(chonkie.TokenChunker(tokenizers.Tokenizer.from_pretrained("gpt2"))) + |
| 56 | + tasks.InformationExtraction(entity_type=Equation, model=model) |
| 57 | + ) |
| 58 | + |
| 59 | + # Define docs to analyze. |
| 60 | + doc = Doc(uri="https://arxiv.org/pdf/1204.0162") |
| 61 | + |
| 62 | + # Run pipeline. |
| 63 | + results = list(pipeline([doc])) |
| 64 | + |
| 65 | + # Print results. |
| 66 | + for equation in results[0].results["InformationExtraction"].entities: |
| 67 | + print(equation) |
0 commit comments