Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Commit

Permalink
build json & pdf reader robustness && ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
hsaeed3 committed Oct 3, 2024
1 parent 1b2629d commit 691d488
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 90 deletions.
44 changes: 21 additions & 23 deletions concepts/textbook-generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,40 @@

# This example will individually import all the necessary modules from zyx
from zyx import (
scrape, # For generating our web research
BaseModel, Field # For generating our textbook & model definitions
scrape, # For generating our web research
BaseModel,
Field, # For generating our textbook & model definitions
)


def generate_textbook(
instructions : str,
model : str = "gpt-4o-mini",
instructions: str,
model: str = "gpt-4o-mini",
) -> BaseModel:

# Lets first collect our information
ai_research = scrape(
instructions,
model = "gpt-4o-mini",
num_queries = 5, # This determines how many queries are generated based on our original query
workers = 5, # This determines how many threads are used for our web search
max_results = 5, # This determines the maximum number of results we scrape from the web
verbose = True # This determines if we want to print out the raw results as we scrape them
model="gpt-4o-mini",
num_queries=5, # This determines how many queries are generated based on our original query
workers=5, # This determines how many threads are used for our web search
max_results=5, # This determines the maximum number of results we scrape from the web
verbose=True, # This determines if we want to print out the raw results as we scrape them
)

# Now that we have our research; lets generate our textbook
# First, we'll have to define our textbook using Pydantic
class TextbookChapter(BaseModel):
title : str = Field(description="The title of the chapter")
content : str = Field(description="The content of the chapter")

title: str = Field(description="The title of the chapter")
content: str = Field(description="The content of the chapter")

class Textbook(BaseModel):
title : str = Field(description="The title of the textbook")
chapters : list[TextbookChapter] = Field(description="The chapters of the textbook")

title: str = Field(description="The title of the textbook")
chapters: list[TextbookChapter] = Field(
description="The chapters of the textbook"
)

textbook = Textbook.generate(
instructions = f"""
instructions=f"""
We have collecting the following information.
<research>
Expand All @@ -50,8 +50,8 @@ class Textbook(BaseModel):
Please use this information to create a comprehensive textbook on {instructions}.
""",
model = "gpt-4o-mini",
process = "sequential" # This generates each field in the model one by one
model="gpt-4o-mini",
process="sequential", # This generates each field in the model one by one
)

# Lets write our textbook to a markdown file
Expand All @@ -63,10 +63,8 @@ class Textbook(BaseModel):


if __name__ == "__main__":

textbook = generate_textbook(
instructions = "AI & LLMs in September 2024",
model = "gpt-4o-mini"
instructions="AI & LLMs in September 2024", model="gpt-4o-mini"
)

print(textbook)
print(textbook)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "zyx"
version = "1.0.10"
version = "1.0.11"
description = "A hyper-fast, fun, quality-of-life focused & genuinely useful LLM toolkit. Inspired by Marvin-AI. Built on LiteLLM, Instructor & Qdrant."
authors = ["Hammad Saeed <hvmmad@gmail.com>"]
readme = "readme.md"
Expand Down
Empty file removed textbook.md
Empty file.
69 changes: 31 additions & 38 deletions zyx/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .lib.utils.logger import get_logger
from .lib.utils.convert_to_openai_tool import convert_to_openai_tool
from instructor import Mode

logger = get_logger(__name__)

Expand All @@ -22,31 +23,6 @@
## -- Instructor Configuration -- ##


## -- Instructor Mode -- ##
## This was directly ported from instructor
## https://github.com/jxnl/instructor/
class Mode(enum.Enum):
"""The mode to use for patching the client"""

FUNCTIONS = "function_call"
PARALLEL_TOOLS = "parallel_tool_call"
TOOLS = "tool_call"
MISTRAL_TOOLS = "mistral_tools"
JSON = "json_mode"
JSON_O1 = "json_o1"
MD_JSON = "markdown_json_mode"
JSON_SCHEMA = "json_schema_mode"
ANTHROPIC_TOOLS = "anthropic_tools"
ANTHROPIC_JSON = "anthropic_json"
COHERE_TOOLS = "cohere_tools"
VERTEXAI_TOOLS = "vertexai_tools"
VERTEXAI_JSON = "vertexai_json"
GEMINI_JSON = "gemini_json"
GEMINI_TOOLS = "gemini_tools"
COHERE_JSON_SCHEMA = "json_object"
TOOLS_STRICT = "tools_strict"


InstructorMode = Literal[
"function_call",
"parallel_tool_call",
Expand Down Expand Up @@ -487,7 +463,7 @@ def __init_client__(self):

return client

def __patch_client__(self):
def __patch_client__(self, mode: Optional[InstructorMode] = "tool_call"):
"""
Patches the client with Instructor.
"""
Expand All @@ -499,12 +475,16 @@ def __patch_client__(self):
if self.provider == "openai":
from instructor import from_openai

patched_client = from_openai(self.clients.client)
patched_client = from_openai(
self.clients.client, mode=get_mode(mode) if mode else None
)

else:
from instructor import patch

patched_client = patch(self.clients.client)
patched_client = patch(
self.clients.client, mode=get_mode(mode) if mode else None
)

if self.config.verbose:
logger.info(f"Patched {self.provider} client with Instructor")
Expand Down Expand Up @@ -594,17 +574,21 @@ def instructor_completion(self, args: CompletionArgs):
if args.tools is None:
exclude_params.update({"tools", "parallel_tool_calls", "tool_choice"})

if self.config.verbose:
logger.info(f"Excluding the following parameters: {exclude_params}")
logger.info(f"Args: {args.model_dump(exclude=exclude_params)}")

if args.model.startswith("o1-"):
logger.warning(
"OpenAI O1- model detected. Removing all non-supported parameters."
)
exclude_params.update(
{
"max_tokens",
"temperature",
"top_p",
"frequency_penalty",
"presence_penalty",
"max_tokens",
"temperature",
"top_p",
"frequency_penalty",
"presence_penalty",
"tools",
"parallel_tool_calls",
"tool_choice",
Expand Down Expand Up @@ -804,7 +788,9 @@ def run_completion(
args = self.execute_tool_call(formatted_tools, args, base_response)

if args:
original_args.messages.extend(args.messages[len(original_args.messages):])
original_args.messages.extend(
args.messages[len(original_args.messages) :]
)
original_args.response_model = response_model
original_args.stream = stream
return self.instructor_completion(original_args)
Expand Down Expand Up @@ -896,9 +882,6 @@ def completion(
model = recommended_model

if response_model:
if not self.clients.instructor:
self.clients.instructor = self.__patch_client__()

mode = get_mode(mode)

if model.startswith("o1-"):
Expand All @@ -907,7 +890,14 @@ def completion(
)
mode = Mode.JSON_O1

self.clients.instructor.mode = mode
if not self.clients.instructor:
self.clients.instructor = self.__patch_client__(mode)

else:
self.clients.instructor.mode = mode

if verbose:
logger.info(f"Instructor Mode: {self.clients.instructor.mode}")

return self.run_completion(
messages=messages,
Expand Down Expand Up @@ -1024,6 +1014,9 @@ def _completion(

client.clients.instructor.mode = mode

if verbose:
logger.info(f"Instructor Mode: {mode}")

return client.run_completion(
messages=messages,
model=model,
Expand Down
4 changes: 3 additions & 1 deletion zyx/resources/completions/agents/scrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
except ImportError:
import os

print("The [bold]`zyx(data)`[/bold] data extension is required to use this module. Install it?")
print(
"The [bold]`zyx(data)`[/bold] data extension is required to use this module. Install it?"
)
if input("Install? (y/n)") == "y":
os.system("pip install 'zyx[data]'")
else:
Expand Down
4 changes: 3 additions & 1 deletion zyx/resources/data/chunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
except ImportError:
import os

print("The [bold]`zyx(data)`[/bold] data extension is required to use this module. Install it?")
print(
"The [bold]`zyx(data)`[/bold] data extension is required to use this module. Install it?"
)
if input("Install? (y/n)") == "y":
os.system("pip install 'zyx[data]'")
else:
Expand Down
Loading

0 comments on commit 691d488

Please sign in to comment.