Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
seanchatmangpt committed Apr 26, 2024
2 parents 20e6ff6 + 0410eb6 commit 9da2529
Show file tree
Hide file tree
Showing 13 changed files with 497 additions and 17 deletions.
1 change: 1 addition & 0 deletions .envDemo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GROQ_API_KEY="gsk_xxx_put_in_your_own_key_xxx"
77 changes: 75 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,36 @@
[![Open in Dev Containers](https://img.shields.io/static/v1?label=Dev%20Containers&message=Open&color=blue&logo=visualstudiocode)](https://vscode.dev/redirect?url=vscode://ms-vscode-remote.remote-containers/cloneInVolume?url=https://github.com/user/my-package)
# DSPyGen: Phyton - for JavaScript use: npm dpgjs
The idea of this fork is to follow up with the genius developer Sean Chatman and his work (https://github.com/seanchatmangpt/dspygen) and find out how to best and quickly get started producing the best quality.

The purpose is to use it mostly everywhere: Expert chatbots, workflows, code/data retrievers...

Thanks, and find out about the great DSPy project here https://github.com/stanfordnlp/dspy.

# My ideas: Structuring Code - A new class of Digital Assets = Dematerialized Commodity
Coming from Financial Engineering and Structuring Financial Products and also from Software Engineering to Avaloq Param, my goals are about getting all Code-Gen Systems to work in compliance and not just mixing others' IP as AI seems mostly to do today.

For such, any creator of something new and valuable should have options to receive a (micro-) payment any time their code is analyzed, cloned, or even used, especially at the enterprise level, where masses of payments might just not be paid.

The bases for all valuable, useful code should then be a new form of NFT - a Structured Commodity of Code - a variant of a Dematerialized Asset - such as I earlier created my Meta-Bricks Repo for.

Having a massive store of runnable and easily pluggable/composable elements of code, paired with terms and conditions we know from classical structured products (see e.g., Ricardian Contracts), Retrievers should use those for Code-Gen workflows, especially to keep legal risks lowest and always send payments or pay shares/revenues from their new meta-bricks derived to the creators owning the underlying product. But we are not here yet, and not many LLMs can and will reference where the code was taken from... I'm sure that can be fixed now.

# Getting Started - Find out simple structures of game code or how creators can get into 'owning' and incentivize code
First: Also in terms of privacy / data loss protection, I try to switch the init to use ollama since this week llama3 came out with decent figures.

To install ollama first go: https://ollama.com/

The default LLM should be set to model="llama3:8b-instruct-q5_1" or "llama3:70b-instruct-q3_K_M"
use
from dspygen.lm.groq_lm import Groq
from dspygen.lm.ollama_lm import Ollama
and the init:
init_dspy(Ollama, model="llama3:8b-instruct-q5_1", max_tokens=8000)
or just run the blog_mudule.py

Use Groq - get the API KEY from https://console.groq.com/keys
and modify your .env like seen in .envDemo
dont forget to init
init_dspy(Groq, model="llama3-70b-8192", max_tokens=8000)

# DSPyGen: Streamlining AI Development

Expand All @@ -17,7 +49,48 @@ DSPyGen, influenced by the efficiency and modularity of Ruby on Rails, is a powe

## Getting Started

Ensure Python is installed on your system. Install DSPyGen via pip:

[![Open in Dev Containers](https://img.shields.io/static/v1?label=Dev%20Containers&message=Open&color=blue&logo=visualstudiocode)](https://vscode.dev/redirect?
url=vscode://ms-vscode-remote.remote-containers/cloneInVolume?url=https://github.com/user/my-package)

or

Ensure Python is installed on your system.

Devs and local changes seems to be always compiled ok with conda for env management

conda create -n dspygen_py python=3.10
conda activate dspygen_py
poetry config virtualenvs.create false

in case needed
poetry update or add <package>

using VS Code - Confirm correct (conda env!) Python Interpreter in VS Code
Ensure that VS Code is using the correct Python interpreter from your virtual environment where dspy-ai is installed:

- Open Command Palette in VS Code: Use Ctrl+Shift+P or Cmd+Shift+P on macOS.
- Select Interpreter: Type and select "Python: Select Interpreter."
- Choose the Correct Environment: Pick the interpreter from the virtual environment associated with your project (\envs\dspygen_py).

pip install -e . develop

on Win get your CLI up to speed and alias

Set-Alias -Name dg -Value dspygen

and

dg --help

should work

try and run the blog-creator and run
src\dspygen\modules\blog_module.py
compare to my run:
src\dspygen\experiments\blog\Tetris_1.md

For production envs:

```bash
pip install dspygen
Expand Down
3 changes: 0 additions & 3 deletions src/dspygen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
import sys
from importlib import import_module, metadata
import subprocess


import os

from pathlib import Path

import inflection
import typer
from munch import Munch

Expand Down
67 changes: 67 additions & 0 deletions src/dspygen/experiments/blog/Tetris_1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# The Tetris Game, Simple but Working
*Running the blog_module.py with Subject "The Tetris Game, simple but working : in 300 lines" on Ollama : llama3:8b-instruct-q5_1 max_tokens=8000

The classic Tetris game is a timeless favorite among gamers. In this article, we will explore how to create a basic version of the game using Python. We'll focus on simplicity and functionality over graphics and advanced features.

## Setting Up the Game Board

First, let's set up our game board as a 2D list with dimensions `10x20`. This will represent the grid where our Tetris pieces will fall.
```python
board = [[' ' for _ in range(20)] for _ in range(10)]
```
## Defining the Tetrominoes

Next, we'll define the different shapes that make up our Tetris pieces. We'll use a list of lists to represent each shape:
```python
tetrominoes = [
[[1, 1], [1, 0]], # I-Shape
[[1, 1, 1], [0, 1, 0]], # O-Shape
[[1, 0, 0], [1, 1, 0]], # T-Shape
[[1, 1, 0], [0, 1, 1]], # L-Shape
[[0, 1, 1], [1, 1, 0]] # J-Shape
]
```
## Generating the Game Loop

Now we'll create a game loop that will handle user input and update our game board accordingly:
```python
while True:
# Get user input (arrow keys)
direction = input("Enter direction (up/down/left/right): ")

# Update the Tetromino's position based on user input
if direction == 'up':
tetromino.y -= 1
elif direction == 'down':
tetromino.y += 1
elif direction == 'left':
tetromino.x -= 1
elif direction == 'right':
tetromino.x += 1

# Check for collisions with the game board and other Tetrominos
if not is_valid_position(tetromino):
print("Game Over!")
break

# Update the game board with the new Tetromino position
update_board(board, tetromino)
```
## Handling Collisions and Game Over

We'll also need to handle collisions between our Tetromino and the game board, as well as other Tetrominos. This will ensure that our game doesn't crash or become stuck:
```python
def is_valid_position(tetromino):
for cell in tetromino.cells:
if (cell.x < 0 or cell.x >= len(board[0]) or
cell.y < 0 or cell.y >= len(board)):
return False
return True

def update_board(board, tetromino):
for cell in tetromino.cells:
board[cell.y][cell.x] = '#'
```
## Conclusion

And that's it! With these simple steps, we've created a basic Tetris game using Python. Of course, there are many ways to improve and expand upon this code, but this should give you a good starting point for your own projects.
98 changes: 98 additions & 0 deletions src/dspygen/experiments/blog/Tetris_LMStud_Llama3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import pygame
import random

# Initialize PyGame
pygame.init()

# Define constants
WIDTH, HEIGHT = 400, 500
BLOCK_SIZE = 20
FPS = 60

# Define colors
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)

class Piece:
def __init__(self):
self.x = 0
self.y = 0
self.matrix = [[1, 1], [1, 1]]

class Game:
def __init__(self):
self.board = [[0 for _ in range(10)] for _ in range(20)]
self.pieces = []
self.score = 0
self.clock = pygame.time.Clock()

def check_collision(self, piece):
for y, row in enumerate(piece.matrix):
for x, val in enumerate(row):
if val == 1:
if (x + piece.x >= 10) or (y + piece.y >= 20) or \
self.board[y + piece.y][x + piece.x] != 0:
return True
return False

def update_screen(self, screen):
for y, row in enumerate(self.board):
for x, val in enumerate(row):
if val != 0:
pygame.draw.rect(screen, BLUE, (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))

def game_loop(self, screen):
while True:
self.clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

piece = Piece()
while True:
piece.x = random.randint(0, 9)
piece.y = 0
if not self.check_collision(piece):
break

while True:
self.update_screen(screen)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
piece.x -= 1
if self.check_collision(piece):
piece.x += 1
if keys[pygame.K_RIGHT]:
piece.x += 1
if self.check_collision(piece):
piece.x -= 1

if random.randint(0, 100) < 10: # Randomly move the piece down
piece.y += 1
if self.check_collision(piece):
for y, row in enumerate(piece.matrix):
for x, val in enumerate(row):
print(val)
if val == 1:
print(x,y)
#self.board[y + piece.y][x + piece.x] = 1
break

for y, row in enumerate(self.board):
if all(val == 1 for val in row):
del self.board[y]
self.score += 10

if __name__ == "__main__":
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Tetris")
game = Game()
game.game_loop(screen)
79 changes: 79 additions & 0 deletions src/dspygen/experiments/blog/Tetris_groq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import pygame
import random

pygame.init()

WIDTH, HEIGHT = 800, 600
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)

class Tetris:
def __init__(self):
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Tetris')
self.clock = pygame.time.Clock()
self.grid = [[0 for _ in range(WIDTH)] for _ in range(HEIGHT)]
self.next_piece = self.get_random_piece()
self.current_piece = self.next_piece
self.next_piece = self.get_random_piece()
self.score = 0
self.x = 0
self.y = 0

def get_random_piece(self):
pieces = ['I', 'J', 'L', 'O', 'S', 'T', 'Z']
return self.get_piece(pieces)

def get_piece(self, pieces):
return random.choice(pieces)

def draw_grid(self):
for i in range(HEIGHT):
for j in range(WIDTH):
if self.grid[i][j] == 1:
pygame.draw.rect(self.screen, BLACK, (j, i, 1, 1))

def draw_current_piece(self):
for i in range(len(self.current_piece)):
for j in range(len(self.current_piece[i])):
if self.current_piece[i][j] == 1:
pygame.draw.rect(self.screen, RED, ((j + self.x, i + self.y), 1, 1))

def move_piece(self):
self.x += 1
if self.x + len(self.current_piece[0]) > WIDTH:
self.x = 0
self.y += 1
if self.y + len(self.current_piece) > HEIGHT:
self.y = 0
self.current_piece = self.next_piece
self.next_piece = self.get_random_piece()
self.score += 1

def run(self):
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.x -= 1
elif event.key == pygame.K_RIGHT:
self.x += 1
elif event.key == pygame.K_DOWN:
self.y += 1
elif event.key == pygame.K_UP:
self.y -= 1
self.move_piece()
self.screen.fill(WHITE)
self.draw_grid()
self.draw_current_piece()
pygame.display.flip()
self.clock.tick(60)
pygame.quit()

if __name__ == '__main__':
game = Tetris()
game.run()
Loading

0 comments on commit 9da2529

Please sign in to comment.