Skip to content

Commit

Permalink
Saving before the storm
Browse files Browse the repository at this point in the history
  • Loading branch information
seanchatmangpt committed Jun 4, 2024
1 parent b5defcd commit 856fa0e
Show file tree
Hide file tree
Showing 11 changed files with 403 additions and 6 deletions.
93 changes: 93 additions & 0 deletions frontend/app/api/unnamed/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { Unnamed } from '@/types/unnamed';

export async function GET(request: Request): Promise<Response> {
const { searchParams } = new URL(request.url);
const id = searchParams.get('id');
const url = id
? `http://localhost:3333/unnameds/${id}`
: 'http://localhost:3333/unnameds';

console.log('GET', id);

const res = await fetch(url);
// Use the Unnamed type for the response
const unnamed: Unnamed | Unnamed[] = await res.json();

console.log(unnamed);

return new Response(JSON.stringify(unnamed), {
headers: { 'Content-Type': 'application/json' },
});
}

export async function POST(request: Request): Promise<Response> {
const unnamedData = await request.json();

const res = await fetch('http://localhost:3333/unnameds', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(unnamedData),
});

const newUnnamed: Unnamed = await res.json();

console.log(newUnnamed);

return new Response(JSON.stringify(newUnnamed), {
headers: { 'Content-Type': 'application/json' },
});
}

export async function PUT(request: Request): Promise<Response> {
const unnamedData = await request.json();
const { searchParams } = new URL(request.url);
const id = searchParams.get('id');

if (!id) {
return new Response(JSON.stringify({ error: 'Unnamed ID is required' }), {
status: 400,
headers: { 'Content-Type': 'application/json' },
});
}

const res = await fetch(`http://localhost:3333/unnameds/${id}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(unnamedData),
});

const updatedUnnamed: Unnamed = await res.json();

console.log(updatedUnnamed);

return new Response(JSON.stringify(updatedUnnamed), {
headers: { 'Content-Type': 'application/json' },
});
}

export async function DELETE(request: Request): Promise<Response> {
const { searchParams } = new URL(request.url);
const id = searchParams.get('id');

if (!id) {
return new Response(
JSON.stringify({ error: 'Unnamed ID is required for deletion' }),
{
status: 400,
headers: { 'Content-Type': 'application/json' },
},
);
}

await fetch(`http://localhost:3333/unnameds/${id}`, {
method: 'DELETE',
});

return new Response(
JSON.stringify({ message: 'Unnamed deleted successfully' }),
{
headers: { 'Content-Type': 'application/json' },
},
);
}
}
3 changes: 3 additions & 0 deletions frontend/types/unnamed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type Unnamed = {

}
91 changes: 91 additions & 0 deletions src/dspygen/agents/swarms/pso.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from enum import Enum, auto
from dspygen.mixin.fsm.fsm_mixin import FSMMixin, trigger
import random
import math


class ParticleState(Enum):
INITIALIZING = auto()
EVALUATING = auto()
UPDATING = auto()
CHECKING_TERMINATION = auto()
TERMINATED = auto()


class Particle(FSMMixin):
def __init__(self, dim, fitness_function):
super().__init__()
self.setup_fsm(state_enum=ParticleState, initial=ParticleState.INITIALIZING)
self.position = [random.uniform(-10, 10) for _ in range(dim)]
self.velocity = [random.uniform(-1, 1) for _ in range(dim)]
self.best_position = list(self.position)
self.best_fitness = float('inf')
self.fitness_function = fitness_function

@trigger(source=ParticleState.INITIALIZING, dest=ParticleState.EVALUATING)
def initialize(self):
print("Initializing particle.")

@trigger(source=ParticleState.EVALUATING, dest=ParticleState.UPDATING)
def evaluate(self):
fitness = self.fitness_function(self.position)
if fitness < self.best_fitness:
self.best_fitness = fitness
self.best_position = list(self.position)
print(f"Evaluating particle. Fitness: {fitness}")

@trigger(source=ParticleState.UPDATING, dest=ParticleState.CHECKING_TERMINATION)
def update(self, global_best_position, inertia=0.5, cognitive=1.5, social=1.5):
for i in range(len(self.position)):
r1 = random.random()
r2 = random.random()
cognitive_velocity = cognitive * r1 * (self.best_position[i] - self.position[i])
social_velocity = social * r2 * (global_best_position[i] - self.position[i])
self.velocity[i] = inertia * self.velocity[i] + cognitive_velocity + social_velocity
self.position[i] += self.velocity[i]
print("Updating particle position and velocity.")

@trigger(source=ParticleState.CHECKING_TERMINATION, dest=[ParticleState.TERMINATED, ParticleState.EVALUATING])
def check_termination(self, iteration, max_iterations):
if iteration >= max_iterations:
print("Termination condition met.")
return ParticleState.TERMINATED
else:
print("Continuing to next iteration.")
return ParticleState.EVALUATING


def fitness_function(position):
return sum(x ** 2 for x in position) # Example fitness function: Sphere function


def main():
dim = 2 # Dimensionality of the problem
num_particles = 5 # Number of particles in the swarm
max_iterations = 100 # Maximum number of iterations

particles = [Particle(dim, fitness_function) for _ in range(num_particles)]
global_best_position = [random.uniform(-10, 10) for _ in range(dim)]
global_best_fitness = float('inf')

for iteration in range(max_iterations):
for particle in particles:
particle.initialize()
particle.evaluate()
if particle.best_fitness < global_best_fitness:
global_best_fitness = particle.best_fitness
global_best_position = list(particle.best_position)
particle.update(global_best_position)
next_state = particle.check_termination(iteration, max_iterations)
if next_state == ParticleState.TERMINATED:
break
else:
particle.to_state(next_state)

print("Optimization completed.")
print(f"Best position: {global_best_position}")
print(f"Best fitness: {global_best_fitness}")


if __name__ == '__main__':
main()
7 changes: 4 additions & 3 deletions src/dspygen/dsl/dsl_pipeline_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,12 @@ async def run_pipeline(request: PipelineRequest):


def main():
context = execute_pipeline('/Users/sac/dev/dspygen/src/dspygen/dsl/examples/example_pipeline.yaml')
# context = execute_pipeline('/Users/sac/dev/dspygen/src/dspygen/dsl/examples/example_pipeline.yaml')
# context = execute_pipeline(str(dsl_dir('examples/text_signature_pipeline.yaml')),
# {"raw_data": "id,name,job\n1,Joe,Coder"})
# context = execute_pipeline(str(dsl_dir('examples/sql_to_nl.yaml')),
# {"query": poor_query})
from dspygen.utils.file_tools import dsl_dir
context = execute_pipeline(str(dsl_dir('examples/sql_to_nl.yaml')),
{"query": "SELECT * FROM table WHERE id = 1"})


print(context)
Expand Down
106 changes: 106 additions & 0 deletions src/dspygen/modules/natural_language_to_sql_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
"""
"""
import dspy
from dspygen.utils.dspy_tools import init_dspy


class NaturalLanguageToSQLModule(dspy.Module):
"""NaturalLanguageToSQLModule"""

def __init__(self, **forward_args):
super().__init__()
self.forward_args = forward_args
self.output = None

def __or__(self, other):
if other.output is None and self.output is None:
self.forward(**self.forward_args)

other.pipe(self.output)

return other

def forward(self, natural_language, database_schema):
pred = dspy.Predict("natural_language, database_schema -> sql_query")
self.output = pred(natural_language=natural_language, database_schema=database_schema).sql_query
return self.output

def pipe(self, input_str):
raise NotImplementedError("Please implement the pipe method for DSL support.")
# Replace TODO with a keyword from you forward method
# return self.forward(TODO=input_str)


from typer import Typer
app = Typer()


@app.command()
def call(natural_language, database_schema):
"""NaturalLanguageToSQLModule"""
init_dspy()

print(natural_language_to_sql_call(natural_language=natural_language, database_schema=database_schema))



def natural_language_to_sql_call(natural_language, database_schema):
natural_language_to_sql = NaturalLanguageToSQLModule()
return natural_language_to_sql.forward(natural_language=natural_language, database_schema=database_schema)

schema = """
CREATE TABLE employees (
id INT PRIMARY KEY,
name TEXT,
department TEXT
organization TEXT
permission_level INT
);
"""

nl = """
Show me the names of all the employees who work in the IT department and have permission level 3.
The must be in the organization 'ABC'.
"""


def main():
init_dspy()
natural_language = nl
database_schema = schema
result = natural_language_to_sql_call(natural_language=natural_language, database_schema=database_schema)
print(result)


from fastapi import APIRouter
router = APIRouter()

@router.post("/natural_language_to_sql/")
async def natural_language_to_sql_route(data: dict):
# Your code generation logic here
init_dspy()

print(data)
return natural_language_to_sql_call(**data)



"""
import streamlit as st
# Streamlit form and display
st.title("NaturalLanguageToSQLModule Generator")
natural_language = st.text_input("Enter natural_language")
database_schema = st.text_input("Enter database_schema")
if st.button("Submit NaturalLanguageToSQLModule"):
init_dspy()
result = natural_language_to_sql_call(natural_language=natural_language, database_schema=database_schema)
st.write(result)
"""

if __name__ == "__main__":
main()
44 changes: 44 additions & 0 deletions src/dspygen/modules/nuxt_page_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""
"""
import dspy
from dspygen.utils.dspy_tools import init_dspy
import os
import subprocess


class NuxtPageModule(dspy.Module):
"""NuxtPageModule"""

def __init__(self, **forward_args):
super().__init__()
self.forward_args = forward_args
self.output = None

def forward(self, requirements):
pred = dspy.Predict("requirements -> nuxt_page_name")
self.output = pred(requirements=requirements).nuxt_page_name
return self.output


def nuxt_page_name_call(requirements):
nuxt_page_name = NuxtPageModule()
return nuxt_page_name.forward(requirements=requirements)


def main():
init_dspy()
requirements = "Todo List"
result = nuxt_page_name_call(requirements=requirements)
print(result)
# Trigger the generation with the result as the name argument
generate_nuxt_page(result)


def generate_nuxt_page(page_name):
os.chdir(os.path.expanduser('~/dev/nuxtgen'))
subprocess.run(['hygen', 'page', 'new', page_name])


if __name__ == "__main__":
main()
11 changes: 8 additions & 3 deletions src/dspygen/rm/code_retriever.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from pathlib import Path
from fnmatch import fnmatch

from dspygen.utils.dspy_tools import init_ol
from dspygen.utils.dspy_tools import init_ol, init_dspy


class CodeRetriever(dspy.Retrieve):
Expand Down Expand Up @@ -85,14 +85,19 @@ def get_files_from_directory(directory, query, gitignore=None):


def main():
path = "/"
# init_ol(model="llava-phi3", max_tokens=2000)
init_dspy(model="gpt-4o", max_tokens=3000)
path = "/Users/sac/dev/nuxt-ai-chatbot/stores"
gitignore = "/.gitignore" # Optional

code_retriever = CodeRetriever(path, gitignore)
result = code_retriever.forward("*.yaml")
result = code_retriever.forward()

for file_content in result.passages:
from dspygen.modules.nuxt_module import nuxt_call
print(file_content)
nuxt = nuxt_call(path=path, readme=file_content)
print(nuxt)
# for file_content in result.passages:
# print(file_content) # Here, you can instead write to a Markdown file or process further.

Expand Down
Loading

0 comments on commit 856fa0e

Please sign in to comment.