-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b5defcd
commit 856fa0e
Showing
11 changed files
with
403 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }, | ||
}, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export type Unnamed = { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.