Skip to content

Commit

Permalink
Code style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
delasy committed Oct 17, 2024
1 parent 7a4d2ed commit 266f7fa
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 195 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/convex.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ jobs:
- name: Deploy Convex 🚀
env:
CONVEX_DEPLOY_KEY: ${{ secrets.CONVEX_DEPLOY_KEY }}
run: npx convex deploy
run: npx convex deploy
2 changes: 0 additions & 2 deletions app/games/[gameId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ export default function GamePage({ params }: { params: { gameId: string } }) {
gameId: params.gameId as Id<"games">,
});

console.log((results));

return (
<div className="container mx-auto max-w-5xl min-h-screen flex flex-col items-center py-12 pb-24 gap-8">
<h1>Game {params.gameId}</h1>
Expand Down
2 changes: 1 addition & 1 deletion app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
--chart-4: 43 74% 66%;
--chart-5: 27 87% 67%;
}

.dark {
--background: 222.2 84% 4.9%;
--foreground: 210 40% 98%;
Expand Down
4 changes: 0 additions & 4 deletions app/maps/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,11 @@ export default function AddMapPage() {
map.forEach((row, y) => {
row.forEach((cell, x) => {
if (map[y][x] == " ") {
console.log("All well!");
flag = true;
}
});
});
console.log(map);
if (!flag) {
console.log("All set");
console.log(map);
alert("No place left to place the player!");
} else {
createMap({ grid: map });
Expand Down
2 changes: 1 addition & 1 deletion app/play/[level]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";

import { useEffect, useState } from "react";
import { useMutation, useQuery, useAction, Authenticated } from "convex/react";
import { useMutation, useQuery, Authenticated } from "convex/react";
import { api } from "@/convex/_generated/api";
import { Button } from "@/components/ui/button";
import { Visualizer } from "../../visualizer";
Expand Down
74 changes: 0 additions & 74 deletions components/MapTester.tsx

This file was deleted.

11 changes: 3 additions & 8 deletions components/ThemeToggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group";
import { DesktopIcon, MoonIcon, SunIcon } from "@radix-ui/react-icons";
import { useState, useEffect } from "react";
import { useTheme } from "next-themes";

export function ThemeToggle() {
Expand All @@ -14,14 +13,10 @@ export function ThemeToggle() {
type="single"
size="sm"
value={theme}
onValueChange={(e) => {
console.log(`theme: ${theme}`);
console.log(`darkMode: ${darkMode}`);
setTheme(e);
}}
className={`${"flex px-1 py-1 rounded-md"} ${theme == "light" || (theme=="system" && darkMode=="light") ? "bg-blue-200" : "bg-slate-700"}`}
onValueChange={(e) => setTheme(e)}
className={`${"flex px-1 py-1 rounded-md"} ${theme == "light" || (theme == "system" && darkMode == "light") ? "bg-blue-200" : "bg-slate-700"}`}
>
{theme == "light" || (theme=="system"&& darkMode=="light") ? (
{theme == "light" || (theme == "system" && darkMode == "light") ? (
<ToggleGroupItem value="dark" aria-label="Dark">
<MoonIcon />
</ToggleGroupItem>
Expand Down
6 changes: 3 additions & 3 deletions convex/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ export const AI_MODELS = [
},
{
model: "claude-3.5-sonnet",
name: "Claude 3.5 Sonnnet"
name: "Claude 3.5 Sonnnet",
},
{
model: "perplexity-llama-3.1",
name: "Perplextity AI"
}
name: "Perplextity AI",
},
];

export const AI_MODEL_IDS = AI_MODELS.map((model) => model.model);
Expand Down
2 changes: 1 addition & 1 deletion convex/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { query } from "./_generated/server";

export const getFlags = query({
args: {},
handler: async (ctx) => {
handler: async () => {
return {
showTestPage: process.env.FLAG_TEST_PAGE === "true",
enableCronJobs: process.env.FLAG_CRON_JOBS === "true",
Expand Down
1 change: 0 additions & 1 deletion convex/maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { v } from "convex/values";
import { ZombieSurvival } from "../simulators/zombie-survival";
import { api, internal } from "./_generated/api";
import { runModel } from "../models";
import { auth } from "./auth";
import { getAuthUserId } from "@convex-dev/auth/server";

const LEVELS = [
Expand Down
80 changes: 37 additions & 43 deletions models/claude-3-5-sonnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,42 @@ import { Anthropic } from "@anthropic-ai/sdk";
import { type ModelHandler } from ".";

export const claude35sonnet: ModelHandler = async (prompt, map) => {
const anthropic = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
});
const anthropic = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
});

try {
const response = await anthropic.messages.create({
model: "claude-3-5-sonnet-20240620",
max_tokens: 1024,
temperature: 0,
system: prompt,
messages: [
{
role: "user",
content: JSON.stringify(map),
},
],
});

const content = response.content[0];

if (content.type !== "text") {
throw new Error("Unexpected response type from Claude");
}

const parsedResponse = JSON.parse(content.text);

// Validate the response structure
if (
!Array.isArray(parsedResponse.boxCoordinates) ||
!Array.isArray(parsedResponse.playerCoordinates) ||
typeof parsedResponse.reasoning !== "string"
) {
throw new Error("Invalid response structure");
}

return {
boxCoordinates: parsedResponse.boxCoordinates,
playerCoordinates: parsedResponse.playerCoordinates,
reasoning: parsedResponse.reasoning,
};
} catch (error) {
console.error("Error in claude35sonnet:", error);
throw error;
}
const response = await anthropic.messages.create({
model: "claude-3-5-sonnet-20240620",
max_tokens: 1024,
temperature: 0,
system: prompt,
messages: [
{
role: "user",
content: JSON.stringify(map),
},
],
});

const content = response.content[0];

if (content.type !== "text") {
throw new Error("Unexpected response type from Claude");
}

const parsedResponse = JSON.parse(content.text);

if (
!Array.isArray(parsedResponse.boxCoordinates) ||
!Array.isArray(parsedResponse.playerCoordinates) ||
typeof parsedResponse.reasoning !== "string"
) {
throw new Error("Invalid response structure");
}

return {
boxCoordinates: parsedResponse.boxCoordinates,
playerCoordinates: parsedResponse.playerCoordinates,
reasoning: parsedResponse.reasoning,
};
};
91 changes: 44 additions & 47 deletions models/perplexity-llama.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from 'zod';
import { ModelHandler } from './index';
import { z } from "zod";
import { ModelHandler } from "./index";

const PerplexityResponseSchema = z.object({
id: z.string(),
Expand All @@ -14,11 +14,13 @@ const PerplexityResponseSchema = z.object({
role: z.string(),
content: z.string(),
}),
delta: z.object({
role: z.string(),
content: z.string(),
}).optional(),
})
delta: z
.object({
role: z.string(),
content: z.string(),
})
.optional(),
}),
),
usage: z.object({
prompt_tokens: z.number(),
Expand All @@ -33,62 +35,57 @@ const GameResponseSchema = z.object({
boxCoordinates: z.array(z.array(z.number())),
});

export const perplexityModel: ModelHandler = async (prompt: string, map: string[][]) => {
const apiKey = process.env.PERPLEXITY_API_KEY;
if (!apiKey) {
throw new Error('PERPLEXITY_API_KEY is not set in the environment variables');
}

export const perplexityModel: ModelHandler = async (
prompt: string,
map: string[][],
) => {
const messages = [
{ role: 'system', content: 'Be precise and concise.' },
{ role: 'user', content: `${prompt}\n\nMap:\n${JSON.stringify(map)}` },
{ role: "system", content: "Be precise and concise." },
{ role: "user", content: `${prompt}\n\nMap:\n${JSON.stringify(map)}` },
];

const data = {
model: 'llama-3.1-sonar-large-128k-online',
model: "llama-3.1-sonar-large-128k-online",
messages,
temperature: 0.2,
top_p: 0.9,
return_citations: true,
search_domain_filter: ['perplexity.ai'],
search_domain_filter: ["perplexity.ai"],
return_images: false,
return_related_questions: false,
search_recency_filter: 'month',
search_recency_filter: "month",
top_k: 0,
stream: false,
presence_penalty: 0,
frequency_penalty: 1,
};

try {
const response = await fetch('https://api.perplexity.ai/chat/completions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
});
const response = await fetch("https://api.perplexity.ai/chat/completions", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.PERPLEXITY_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});

if (!response.ok) {
const errorData = await response.json();
console.error('API Error:', errorData);
throw new Error(`HTTP error! status: ${response.status}, message: ${JSON.stringify(errorData)}`);
}
if (!response.ok) {
const errorData = await response.json();

const responseData = await response.json();
const validatedResponse = PerplexityResponseSchema.parse(responseData);
const content = validatedResponse.choices[0].message.content;
const parsedContent = JSON.parse(content);
const gameResponse = GameResponseSchema.parse(parsedContent);

return {
boxCoordinates: gameResponse.boxCoordinates,
playerCoordinates: gameResponse.playerCoordinates,
reasoning: gameResponse.reasoning,
};
} catch (error) {
console.error('Failed to run Perplexity model Error:', error);
throw error;
throw new Error(
`HTTP error! status: ${response.status}, message: ${JSON.stringify(errorData)}`,
);
}
};

const responseData = await response.json();
const validatedResponse = PerplexityResponseSchema.parse(responseData);
const content = validatedResponse.choices[0].message.content;
const parsedContent = JSON.parse(content);
const gameResponse = GameResponseSchema.parse(parsedContent);

return {
boxCoordinates: gameResponse.boxCoordinates,
playerCoordinates: gameResponse.playerCoordinates,
reasoning: gameResponse.reasoning,
};
};
Loading

0 comments on commit 266f7fa

Please sign in to comment.