Skip to content

Commit

Permalink
Made examples use tensorlake_function and tensorlake_router
Browse files Browse the repository at this point in the history
  • Loading branch information
Julio Martinez authored and eabatalov committed Jan 14, 2025
1 parent 3976d97 commit 37b3b1d
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 50 deletions.
4 changes: 2 additions & 2 deletions examples/contextual_rag/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from tensorlake.functions_sdk.graph import Graph
from tensorlake.functions_sdk.image import Image
from tensorlake.functions_sdk.functions import indexify_function, TensorlakeCompute
from tensorlake.functions_sdk.functions import tensorlake_function, TensorlakeCompute

# TODO User set this
contextual_retrieval_prompt = """
Expand Down Expand Up @@ -43,7 +43,7 @@ class ChunkContext(BaseModel):
chunks: List[str]
chunk_contexts: List[str]

@indexify_function(image=image)
@tensorlake_function(image=image)
def generate_chunk_contexts(doc: str) -> ChunkContext:
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=750,
Expand Down
2 changes: 1 addition & 1 deletion examples/ditributed_map.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pydantic import BaseModel
from tensorlake import tensorlake_function, indexify_router, Graph
from tensorlake import tensorlake_function, tensorlake_router, Graph
from typing import List, Union

@tensorlake_function()
Expand Down
14 changes: 7 additions & 7 deletions examples/knowledge_graph/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pydantic import BaseModel, Field
from tensorlake import RemoteGraph
from tensorlake.functions_sdk.graph import Graph
from tensorlake.functions_sdk.functions import indexify_function, TensorlakeCompute
from tensorlake.functions_sdk.functions import tensorlake_function, TensorlakeCompute
from tensorlake.functions_sdk.image import Image
from neo4j import GraphDatabase
import json
Expand Down Expand Up @@ -187,7 +187,7 @@ def run(self, data: Tuple[List[Entity], str, Document]) -> Tuple[List[Entity], L
logging.error(f"Error in extract_relationships: {str(e)}")
raise

@indexify_function(image=base_image)
@tensorlake_function(image=base_image)
def build_knowledge_graph(data: Tuple[List[Entity], List[Relationship], Document]) -> KnowledgeGraphOutput:
try:
entities, relationships, doc = data
Expand All @@ -199,7 +199,7 @@ def build_knowledge_graph(data: Tuple[List[Entity], List[Relationship], Document
logging.error(f"Error in build_knowledge_graph: {str(e)}", "knowledge_graph_output --->")
raise

@indexify_function(image=neo4j_image)
@tensorlake_function(image=neo4j_image)
def store_in_neo4j(data: KnowledgeGraphOutput) -> bool:
try:
kg = data.knowledge_graph
Expand All @@ -226,7 +226,7 @@ def store_in_neo4j(data: KnowledgeGraphOutput) -> bool:
logging.error(f"Error in store_in_neo4j: {str(e)}")
raise

@indexify_function(image=embedding_image)
@tensorlake_function(image=embedding_image)
def generate_embeddings(data: KnowledgeGraphOutput) -> TextChunk:
try:
doc = data.document
Expand All @@ -246,7 +246,7 @@ def generate_embeddings(data: KnowledgeGraphOutput) -> TextChunk:
logging.error(f"Error in generate_embeddings: {str(e)}")
raise

@indexify_function(image=gemini_image)
@tensorlake_function(image=gemini_image)
def question_to_cypher(question: Question) -> CypherQueryAndQuestion:
try:
model = genai.GenerativeModel("gemini-pro")
Expand Down Expand Up @@ -274,7 +274,7 @@ def question_to_cypher(question: Question) -> CypherQueryAndQuestion:
logging.error(f"Error in question_to_cypher: {str(e)}")
raise

@indexify_function(image=neo4j_image)
@tensorlake_function(image=neo4j_image)
def execute_cypher_query(data: CypherQueryAndQuestion) -> QuestionAndResult:
cypher_query, question = data.cypher_query, data.question
uri = os.getenv('NEO4J_URI', "bolt://localhost:7687")
Expand All @@ -294,7 +294,7 @@ def execute_cypher_query(data: CypherQueryAndQuestion) -> QuestionAndResult:

return QuestionAndResult(question=question, query_result=QueryResult(result=records))

@indexify_function(image=gemini_image)
@tensorlake_function(image=gemini_image)
def generate_answer(data: QuestionAndResult) -> Answer:
query_result, question = data.query_result, data.question

Expand Down
2 changes: 1 addition & 1 deletion examples/object_detection/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from tensorlake.functions_sdk.data_objects import File
from tensorlake.functions_sdk.functions import (
TensorlakeCompute,
indexify_function,
tensorlake_function,
)
from pydantic import BaseModel
from typing import List
Expand Down
6 changes: 3 additions & 3 deletions examples/pdf_document_extraction/embedding.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from typing import Any, List

from tensorlake.functions_sdk.functions import TensorlakeCompute, indexify_function
from tensorlake.functions_sdk.functions import TensorlakeCompute, tensorlake_function
from sentence_transformers import SentenceTransformer
from common_objects import ImageWithEmbedding, TextChunk, PDFParserDoclingOutput
from inkwell.api.document import Document
from inkwell.api.page import PageFragmentType
import base64
from images import st_image

@indexify_function(image=st_image)
@tensorlake_function(image=st_image)
def chunk_text(document: Document) -> List[TextChunk]:
"""
Extract chunks from document
Expand Down Expand Up @@ -40,7 +40,7 @@ def chunk_text(document: Document) -> List[TextChunk]:
return chunks


@indexify_function(image=st_image)
@tensorlake_function(image=st_image)
def chunk_text_docling(document: PDFParserDoclingOutput) -> List[TextChunk]:
"""
Extract chunks from documents
Expand Down
4 changes: 2 additions & 2 deletions examples/pdf_document_extraction/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from tensorlake import RemoteGraph
from tensorlake.functions_sdk.data_objects import File
from tensorlake.functions_sdk.graph import Graph
from tensorlake.functions_sdk.functions import indexify_function
from tensorlake.functions_sdk.functions import tensorlake_function
from images import http_client_image

@indexify_function(image=http_client_image)
@tensorlake_function(image=http_client_image)
def download_pdf(url: str) -> File:
"""
Download pdf from url
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from decimal import Decimal
from datetime import date
from tensorlake.functions_sdk.data_objects import File
from tensorlake import indexify_function, Graph, RemoteGraph, Image
from tensorlake import tensorlake_function, Graph, RemoteGraph, Image
from typing import Optional, List, Any


Expand All @@ -18,7 +18,7 @@
)


@indexify_function(image=image)
@tensorlake_function(image=image)
def tensorlake_document_ai_parse(file: File) -> str:
"""
Call the inkwell parse API. TODO replace the token below for `Bearer`.
Expand Down Expand Up @@ -120,7 +120,7 @@ def _call_oai_client(system_prompt: str, user_prompt: str, markdown: str) -> str
return response.choices[0].message.content


@indexify_function(image=image)
@tensorlake_function(image=image)
def extract_with_oai(markdown: str) -> BillSchema:
schema = BillSchema.model_json_schema()

Expand Down
6 changes: 3 additions & 3 deletions examples/pdf_structured_extraction/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from datetime import date
import tempfile
from tensorlake.functions_sdk.data_objects import File
from tensorlake import indexify_function, Graph, RemoteGraph, Image
from tensorlake import tensorlake_function, Graph, RemoteGraph, Image
from typing import Optional

image = (
Expand Down Expand Up @@ -37,7 +37,7 @@ class Config:
Decimal: lambda v: str(v)
}

@indexify_function(image=image)
@tensorlake_function(image=image)
def parse_pdf(file: File) -> BillSchema:
from inkwell.ocr import OCRType, OCRFactory
from inkwell.io import read_pdf_as_images
Expand All @@ -61,7 +61,7 @@ def parse_pdf(file: File) -> BillSchema:
print(result)
return BillSchema.model_validate_json(result)

@indexify_function(image=image)
@tensorlake_function(image=image)
def write_to_db(bill: BillSchema) -> None:
from sqlmodel import Field, SQLModel, Session, create_engine
from typing import Optional
Expand Down
6 changes: 3 additions & 3 deletions examples/readme/distributed_map.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from pydantic import BaseModel
from tensorlake import indexify_function, indexify_router, Graph
from tensorlake import tensorlake_function, tensorlake_router, Graph
from typing import List, Union

@indexify_function()
@tensorlake_function()
def generate_sequence(a: int) -> List[int]:
return [i for i in range(a)]

@indexify_function()
@tensorlake_function()
def squared(x: int) -> int:
return x * x

Expand Down
8 changes: 4 additions & 4 deletions examples/readme/readme_example.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
from pydantic import BaseModel
from tensorlake import indexify_function, Graph
from tensorlake import tensorlake_function, Graph
from typing import List

class Total(BaseModel):
val: int = 0

@indexify_function()
@tensorlake_function()
def generate_numbers(a: int) -> List[int]:
return [i for i in range(a)]

@indexify_function()
@tensorlake_function()
def square(x: int) -> int:
return x ** 2

@indexify_function(accumulate=Total)
@tensorlake_function(accumulate=Total)
def add(total: Total, new: int) -> Total:
total.val += new
return total
Expand Down
8 changes: 4 additions & 4 deletions examples/readme/website.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from tensorlake import indexify_function, Graph
from tensorlake import tensorlake_function, Graph
from pydantic import BaseModel

class Audio(BaseModel):
file: bytes

@indexify_function()
@tensorlake_function()
def scrape_website(url: str) -> str:
import requests
return requests.get(f"http://r.jina.ai/{url}").text

@indexify_function()
@tensorlake_function()
def summarize_text(text: str) -> str:
from openai import OpenAI
completion = OpenAI().chat.completions.create(
Expand All @@ -21,7 +21,7 @@ def summarize_text(text: str) -> str:
)
return completion.choices[0].message.content

@indexify_function()
@tensorlake_function()
def create_audio(summary: str) -> Audio:
import elevenlabs
from elevenlabs import save
Expand Down
10 changes: 5 additions & 5 deletions examples/tweetsgenerator/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pydantic import BaseModel, Field
from tensorlake import RemoteGraph
from tensorlake.functions_sdk.graph import Graph
from tensorlake.functions_sdk.functions import indexify_function
from tensorlake.functions_sdk.functions import tensorlake_function
from tensorlake.functions_sdk.image import Image

# Set up logging
Expand Down Expand Up @@ -51,7 +51,7 @@ class RankedTweets(BaseModel):
)


@indexify_function(image=openai_image_3_10)
@tensorlake_function(image=openai_image_3_10)
def generate_tweet_topics(subject: str) -> List[str]:
"""Generate topics for tweets about a given subject."""
import openai
Expand All @@ -74,7 +74,7 @@ class Topics(BaseModel):
topics = response.choices[0].message.parsed
return topics.topics

@indexify_function(image=openai_image_3_10)
@tensorlake_function(image=openai_image_3_10)
def generate_tweet(topic: str) -> str:
"""Generate a tweet about a given topic."""
import openai
Expand All @@ -96,13 +96,13 @@ class Tweet(BaseModel):
tweet = response.choices[0].message.parsed
return tweet.tweet

@indexify_function(image=base_image_3_10, accumulate=Tweets)
@tensorlake_function(image=base_image_3_10, accumulate=Tweets)
def accumulate_tweets(acc: Tweets, tweet: str) -> Tweets:
"""Accumulate generated tweets."""
acc.tweets.append(tweet)
return acc

@indexify_function(image=openai_image_3_10)
@tensorlake_function(image=openai_image_3_10)
def score_and_rank_tweets(tweets: Tweets) -> RankedTweets:
"""Score and rank the accumulated tweets."""
import openai
Expand Down
16 changes: 8 additions & 8 deletions examples/video_summarization/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from tensorlake.functions_sdk.data_objects import File
from tensorlake.functions_sdk.graph import Graph
from tensorlake.functions_sdk.image import Image
from tensorlake.functions_sdk.functions import indexify_function, indexify_router
from tensorlake.functions_sdk.functions import tensorlake_function, tensorlake_router

# Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
Expand Down Expand Up @@ -58,7 +58,7 @@ class Summary(BaseModel):
)

# Indexify Functions
@indexify_function(image=yt_downloader_image)
@tensorlake_function(image=yt_downloader_image)
def download_youtube_video(url: YoutubeURL) -> List[File]:
"""Download the YouTube video from the URL."""
from pytubefix import YouTube
Expand All @@ -68,14 +68,14 @@ def download_youtube_video(url: YoutubeURL) -> List[File]:
logging.info("Video downloaded")
return [File(data=content, mime_type="video/mp4")]

@indexify_function(image=audio_image)
@tensorlake_function(image=audio_image)
def extract_audio_from_video(file: File) -> File:
"""Extract the audio from the video."""
from pydub import AudioSegment
audio = AudioSegment.from_file(file.data)
return File(data=audio.export("audio.wav", format="wav").read(), mime_type="audio/wav")

@indexify_function(image=transcribe_image)
@tensorlake_function(image=transcribe_image)
def transcribe_audio(file: File) -> Transcription:
"""Transcribe audio and diarize speakers."""
from faster_whisper import WhisperModel
Expand All @@ -84,7 +84,7 @@ def transcribe_audio(file: File) -> Transcription:
audio_segments = [SpeechSegment(text=segment.text, start_ts=segment.start, end_ts=segment.end) for segment in segments]
return Transcription(segments=audio_segments)

@indexify_function(image=llama_cpp_image)
@tensorlake_function(image=llama_cpp_image)
def classify_meeting_intent(speech: Transcription) -> Transcription:
try:
"""Classify the intent of the audio."""
Expand Down Expand Up @@ -125,7 +125,7 @@ def classify_meeting_intent(speech: Transcription) -> Transcription:
speech.classification = SpeechClassification(classification="unknown", confidence=0.5)
return speech

@indexify_function(image=llama_cpp_image)
@tensorlake_function(image=llama_cpp_image)
def summarize_job_interview(speech: Transcription) -> Summary:
"""Summarize the job interview."""
from llama_cpp import Llama
Expand All @@ -151,7 +151,7 @@ def summarize_job_interview(speech: Transcription) -> Summary:
output = model(prompt=prompt, max_tokens=30000, stop=["\n"])
return Summary(summary=output["choices"][0]["text"])

@indexify_function(image=llama_cpp_image)
@tensorlake_function(image=llama_cpp_image)
def summarize_sales_call(speech: Transcription) -> Summary:
"""Summarize the sales call."""
from llama_cpp import Llama
Expand All @@ -177,7 +177,7 @@ def summarize_sales_call(speech: Transcription) -> Summary:
output = model(prompt=prompt, max_tokens=30000, stop=["\n"])
return Summary(summary=output["choices"][0]["text"])

@indexify_router(image=base_image)
@tensorlake_router(image=base_image)
def route_transcription_to_summarizer(speech: Transcription) -> List[Union[summarize_job_interview, summarize_sales_call]]:
"""Route the transcription to the appropriate summarizer based on the classification."""
if speech.classification.classification == "job-interview":
Expand Down
Loading

0 comments on commit 37b3b1d

Please sign in to comment.