Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Gianni Crivello authored and Gianni Crivello committed Aug 21, 2024
1 parent f54cd2a commit 2962530
Show file tree
Hide file tree
Showing 27 changed files with 2,063 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Deploy to GitHub Pages

permissions:
contents: write
pages: write

on:
push:
branches: [ "main", "master" ]
workflow_dispatch:
jobs:
deploy:
runs-on: ubuntu-latest
steps: [uses: fastai/workflows/quarto-ghp@master]
7 changes: 7 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: CI
on: [workflow_dispatch, pull_request, push]

jobs:
test:
runs-on: ubuntu-latest
steps: [uses: fastai/workflows/nbdev-ci@master]
151 changes: 151 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
_docs/
_proc/

*.bak
.gitattributes
.last_checked
.gitconfig
*.bak
*.log
*~
~*
_tmp*
tmp*
tags
*.pkg

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# dotenv
.env

# virtualenv
.venv
venv/
ENV/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/

.vscode
*.swp

# osx generated files
.DS_Store
.DS_Store?
.Trashes
ehthumbs.db
Thumbs.db
.idea

# pytest
.pytest_cache

# tools/trust-doc-nbs
docs_src/.last_checked

# symlinks to fastai
docs_src/fastai
tools/fastai

# link checker
checklink/cookies.txt

# .gitconfig is now autogenerated
.gitconfig

# Quarto installer
.deb
.pkg

# Quarto
.quarto
5 changes: 5 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include settings.ini
include LICENSE
include CONTRIBUTING.md
include README.md
recursive-exclude * __pycache__
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file will be overwritten by `index.ipynb`
1 change: 1 addition & 0 deletions lazy_lm/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "0.0.1"
24 changes: 24 additions & 0 deletions lazy_lm/_modidx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Autogenerated by nbdev

d = { 'settings': { 'branch': 'main',
'doc_baseurl': '/lazy_lm',
'doc_host': 'https://Techolution.github.io',
'git_url': 'https://github.com/Techolution/lazy_lm',
'lib_path': 'lazy_lm'},
'syms': { 'lazy_lm.core': { 'lazy_lm.core.AnthropicVertex.lazy': ('core.html#anthropicvertex.lazy', 'lazy_lm/core.py'),
'lazy_lm.core.LLM': ('core.html#llm', 'lazy_lm/core.py'),
'lazy_lm.core.LazyEvaluationClient': ('core.html#lazyevaluationclient', 'lazy_lm/core.py'),
'lazy_lm.core.LazyEvaluationClient.__init__': ('core.html#lazyevaluationclient.__init__', 'lazy_lm/core.py'),
'lazy_lm.core.LazyEvaluationClient.ask_question': ( 'core.html#lazyevaluationclient.ask_question',
'lazy_lm/core.py'),
'lazy_lm.core.LazyEvaluationClient.get_current_step': ( 'core.html#lazyevaluationclient.get_current_step',
'lazy_lm/core.py'),
'lazy_lm.core.LazyEvaluationClient.get_next_step': ( 'core.html#lazyevaluationclient.get_next_step',
'lazy_lm/core.py'),
'lazy_lm.core.LazyEvaluationClient.initalize_problem': ( 'core.html#lazyevaluationclient.initalize_problem',
'lazy_lm/core.py'),
'lazy_lm.core.LazyState': ('core.html#lazystate', 'lazy_lm/core.py'),
'lazy_lm.core.LazyState.__post_init__': ('core.html#lazystate.__post_init__', 'lazy_lm/core.py'),
'lazy_lm.core.LazyState.add_step': ('core.html#lazystate.add_step', 'lazy_lm/core.py'),
'lazy_lm.core.LazyState.get_context': ('core.html#lazystate.get_context', 'lazy_lm/core.py'),
'lazy_lm.core.LazyState.refresh': ('core.html#lazystate.refresh', 'lazy_lm/core.py')}}}
164 changes: 164 additions & 0 deletions lazy_lm/core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/00_core.ipynb.

# %% auto 0
__all__ = ['lazy_system_p', 'LazyState', 'LLM', 'LazyEvaluationClient']

# %% ../nbs/00_core.ipynb 3
from dotenv import load_dotenv
import os
from anthropic import AnthropicVertex
from anthropic.types import (
MessageParam,
Message
)
from fastcore.basics import *
from fastcore.test import *
from fastcore.foundation import *
from dataclasses import (
dataclass,
field
)
from typing import (
List,
Optional
)
from nbdev.showdoc import show_doc

# %% ../nbs/00_core.ipynb 7
@dataclass
class LazyState:
problem: str
steps: List[str] = field(default_factory=list)
current_step: int = 0

def __post_init__(self):
self.steps.append(self.problem)

def add_step(self, step: str):
self.steps.append(step)
self.current_step += 1

def get_context(self) -> str:
return f"Problem: {self.problem} \n Steps so far: {self.steps}"

def refresh(self) -> None:
self.current_step = 0
self.steps = [self.problem]

# %% ../nbs/00_core.ipynb 11
@dataclass
class LLM:
client: AnthropicVertex
model: str

# %% ../nbs/00_core.ipynb 13
lazy_system_p = """
You are a helpful assistant that can help with math problems.
You will be given a problem and a list of steps as context, the format will be:
PROBLEM: <problem>
STEPS: <steps>
Your job is to complete the next step and only the next step in the problem-solving process. You should never give more than one step.
If you evaluate that the problem is done, respond with "PROBLEM DONE"
"""

# %% ../nbs/00_core.ipynb 15
class LazyEvaluationClient:
"""The Lazy Evaluation Client"""
def __init__(self,
llm: LLM, # the language model to use, see `LLM` class
max_tokens: int = 100, # the maximum number of tokens to generate
state: Optional[LazyState] = None,
lazy_system_p: str = lazy_system_p
):
self.model = llm.model
self.client = llm.client
self.max_tokens = max_tokens
self.state = state
self.lazy_system_p = lazy_system_p
self.question_history = []

def initalize_problem(self, problem: str) -> None:
self.state = LazyState(problem=problem)

def get_current_step(self) -> str:
return self.state.steps[self.state.current_step]

def get_next_step(self) -> str:
if self.state is None:
raise ValueError("Problem is not initialized, call initalize_problem first")

messages: List[MessageParam] = [
{
"role": "user",
"content": self.state.get_context()
}
]

response: Message = self.client.messages.create(
system=self.lazy_system_p,
model=self.model,
messages=messages,
max_tokens=self.max_tokens
)
next_step = response.content[0].text
if next_step is not None:
self.state.add_step(next_step.strip())
return next_step.strip()
else:
raise ValueError("No next step found")

# %% ../nbs/00_core.ipynb 27
@patch
def ask_question(self:LazyEvaluationClient, question:str) -> Message:
"""
Allows the user to ask a question about the current step without affecting the model's ability to generate the next step.
Args:
question (str): The question the user wants to ask about the current step.
Returns:
str: The model's response to the question.
"""

current_state = f"""
System: {self.lazy_system_p}
Problem: {self.state.problem}\n
Context: {self.state.get_context()}
Current step: {self.state.steps[self.state.current_step]}
"""
prompt = f"""
Question History: {self.question_history}
Question: {question}\n
Please answer the question without advancing to the next step.
If you are asked to provide an example for a specific step, please provide an example that is not in the current context.
"""

messages: List[MessageParam] = [
{
"role": "user",
"content": prompt
}
]

response: Message = self.client.messages.create(
system=current_state,
model=self.model,
messages=messages,
max_tokens=self.max_tokens
)
self.question_history.append(question)
self.question_history.append(response.content[0].text.strip())

return response.content[0].text.strip()

# %% ../nbs/00_core.ipynb 32
@patch
def lazy(self: AnthropicVertex, problem: str) -> LazyEvaluationClient:
"""
Entry point of the LazyLM Framework for the `AnthropicVertex` client API
"""
state = LazyState(problem=problem)
llm = LLM(client=self, model=model)
return LazyEvaluationClient(llm=llm, state=state)
14 changes: 14 additions & 0 deletions nbdev-template-1.1.2/.github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Deploy to GitHub Pages

permissions:
contents: write
pages: write

on:
push:
branches: [ "main", "master" ]
workflow_dispatch:
jobs:
deploy:
runs-on: ubuntu-latest
steps: [uses: fastai/workflows/quarto-ghp@master]
7 changes: 7 additions & 0 deletions nbdev-template-1.1.2/.github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: CI
on: [workflow_dispatch, pull_request, push]

jobs:
test:
runs-on: ubuntu-latest
steps: [uses: fastai/workflows/nbdev-ci@master]
Loading

0 comments on commit 2962530

Please sign in to comment.