-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from nprasad2077/playoffs
Playoffs
- Loading branch information
Showing
17 changed files
with
1,055 additions
and
2 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
Empty file.
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 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
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,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class GraphqlInputConfig(AppConfig): | ||
default_auto_field = 'django.db.models.BigAutoField' | ||
name = 'graphql_input' |
Empty file.
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 @@ | ||
from django.db import models | ||
|
||
# Create your models here. |
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 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
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,256 @@ | ||
from openai import OpenAI | ||
import os | ||
|
||
client = OpenAI(api_key=os.getenv('OPEN_AI_KEY')) | ||
|
||
from dotenv import load_dotenv | ||
from django.http import JsonResponse | ||
from django.views import View | ||
from django.conf import settings | ||
from graphql_api.models import PlayerDataTotals, PlayerDataAdvanced | ||
|
||
# Load environment variables from .env file | ||
load_dotenv() | ||
|
||
# Set OpenAI API key | ||
|
||
class GenerateGraphQLQueryView(View): | ||
def post(self, request, *args, **kwargs): | ||
user_input = request.POST.get('user_input') | ||
print(f"User Input: {user_input}") # Debug: Print user input | ||
|
||
# Fetch some relevant data from your models to provide context to GPT-3 | ||
example_stats = PlayerDataTotals.objects.filter(team='HOU', season=2019)[:5] | ||
example_stats_adv = PlayerDataAdvanced.objects.filter(team='LAL', season=2006)[:5] | ||
#print(f"Fetched {len(example_stats)} example stats") # Debug: Print number of fetched stats | ||
|
||
example_data = "" | ||
|
||
''' | ||
for stat in example_stats: | ||
example_data += f"- Player: {stat.player_name}, Points: {stat.points}, Assists: {stat.assists}, Games: {stat.games}, Position: {stat.position}\n" | ||
''' | ||
# print(f"Example Data: {example_data}") # Debug: Print example data | ||
|
||
# Define the GraphQL schema for the AI assistant | ||
graphql_schema = """ | ||
type Query { | ||
playerDataTotals(team: String, season: Int, ordering: String, limit: Int, name: String, id: Int, playerId: String): [PlayerDataTotals] | ||
playerDataTotalsPlayoffs(team: String, season: Int, ordering: String, limit: Int, name: String, id: Int, playerId: String): [PlayerDataTotalsPlayoffs] | ||
playerDataAdvanced(team: String, season: Int, ordering: String, limit: Int, name: String, id: Int, playerId: String): [PlayerDataAdvanced] | ||
playerDataAdvancedPlayoffs(team: String, season: Int, ordering: String, limit: Int, name: String, id: Int, playerId: String): [PlayerDataAdvancedPlayoffs] | ||
} | ||
type PlayerDataTotals { | ||
player_name: String | ||
points: Int | ||
assists: Int | ||
games: Int | ||
position: String | ||
team: String | ||
season: Int | ||
playerId: String | ||
id: Int | ||
totalRb: Int: Int | ||
offensiveRb: Int | ||
defensiveRb: Int | ||
blocks: Int | ||
steals: Int | ||
turnovers: Int | ||
ft: Int | ||
ftAttempts: Decimal | ||
ftPercent: Decimal | ||
twoAttempts: Int | ||
twoFg: Int | ||
twoPercent: Decimal | ||
threeAttempts: Int | ||
threeFg: Int | ||
threePercent: Decimal | ||
effectFgPercent: Decimal | ||
fieldAttempts: Int | ||
fieldGoals: Int | ||
fieldPercent: Decimal | ||
personalFouls: Int | ||
minutesPg: Int | ||
gamesStarted: Int | ||
} | ||
type PLayerDataAdvanced { | ||
id: Int | ||
playerId: String | ||
playerName: String | ||
position: String | ||
team: String | ||
season: Int | ||
games: Int | ||
minutesPlayed: Int | ||
winShares: Decimal | ||
offensiveWs: Decimal | ||
defensiveWs: Decimal | ||
winSharesPer: Decimal | ||
box: Decimal | ||
offensiveBox: Decimal | ||
defensiveBox: Decimal | ||
per: Decimal | ||
vorp: Decimal | ||
usagePercent: Decimal | ||
tsPercent: Decimal | ||
ftr: Decimal | ||
assistPercent: Decimal | ||
blockPercent: Decimal | ||
totalRbPercent: Decimal | ||
offensiveRbPercent: Decimal | ||
defensiveRbPercent: Decimal | ||
stealPercent: Decimal | ||
threePAr: Decimal | ||
turnoverPercent: Decimal | ||
age: Int | ||
} | ||
type PlayerDataTotalsPlayoffs { | ||
player_name: String | ||
points: Int | ||
assists: Int | ||
games: Int | ||
position: String | ||
team: String | ||
season: Int | ||
playerId: String | ||
id: Int | ||
totalRb: Int: Int | ||
offensiveRb: Int | ||
defensiveRb: Int | ||
blocks: Int | ||
steals: Int | ||
turnovers: Int | ||
ft: Int | ||
ftAttempts: Decimal | ||
ftPercent: Decimal | ||
twoAttempts: Int | ||
twoFg: Int | ||
twoPercent: Decimal | ||
threeAttempts: Int | ||
threeFg: Int | ||
threePercent: Decimal | ||
effectFgPercent: Decimal | ||
fieldAttempts: Int | ||
fieldGoals: Int | ||
fieldPercent: Decimal | ||
personalFouls: Int | ||
minutesPg: Int | ||
gamesStarted: Int | ||
} | ||
type PLayerDataAdvancedPlayoffs { | ||
id: Int | ||
playerId: String | ||
playerName: String | ||
position: String | ||
team: String | ||
season: Int | ||
games: Int | ||
minutesPlayed: Int | ||
winShares: Decimal | ||
offensiveWs: Decimal | ||
defensiveWs: Decimal | ||
winSharesPer: Decimal | ||
box: Decimal | ||
offensiveBox: Decimal | ||
defensiveBox: Decimal | ||
per: Decimal | ||
vorp: Decimal | ||
usagePercent: Decimal | ||
tsPercent: Decimal | ||
ftr: Decimal | ||
assistPercent: Decimal | ||
blockPercent: Decimal | ||
totalRbPercent: Decimal | ||
offensiveRbPercent: Decimal | ||
defensiveRbPercent: Decimal | ||
stealPercent: Decimal | ||
threePAr: Decimal | ||
turnoverPercent: Decimal | ||
age: Int | ||
} | ||
""" | ||
|
||
# Construct the prompt with additional context | ||
prompt = f""" | ||
User input: "{user_input}" | ||
Given the following player stats data: | ||
{example_stats} | ||
{example_stats_adv} | ||
GraphQL Schema: | ||
{graphql_schema} | ||
Please generate a valid GraphQL query based on the user input. Ensure the 'ordering' variable is a single word with a '-' sign in front, indicating the field to order by in descending order. | ||
Always use NBA team abbreviations for the 'team' field. Convert all user input to NBA team names in query. | ||
If a field is not provided do not include it. Always return all fields unless specified otherwise in the user input. | ||
Example: | ||
query {{ | ||
playerDataTotals(team: "LAL", season: 2000, ordering: "-points", limit: 20) {{ | ||
player_name | ||
points | ||
assists | ||
games | ||
position | ||
team | ||
season | ||
playerId | ||
id | ||
totalRb | ||
offensiveRb | ||
defensiveRb | ||
blocks | ||
steals | ||
turnovers | ||
ft | ||
ftAttempts | ||
ftPercent | ||
twoAttempts | ||
twoFg | ||
twoPercent | ||
threeAttempts | ||
threeFg | ||
threePercent | ||
effectFgPercent | ||
fieldAttempts | ||
fieldGoals | ||
fieldPercent | ||
personalFouls | ||
minutesPg | ||
gamesStarted | ||
}} | ||
}} | ||
GraphQL query: | ||
""" | ||
#print(f"Prompt: {prompt}") # Debug: Print the prompt | ||
|
||
try: | ||
response = client.chat.completions.create( | ||
model="gpt-4-turbo", # Updated to use the 'gpt-4' model | ||
messages=[ | ||
{"role": "system", "content": "You are an assistant that helps generate GraphQL queries."}, | ||
{"role": "user", "content": prompt} | ||
], | ||
max_tokens=1000, | ||
n=1, | ||
stop=["\n\n"], # You can specify stop sequences to prevent the model from going off-topic | ||
temperature=0.7 | ||
) | ||
# print(f"OpenAI Response: {response}") # Debug: Print the raw response | ||
|
||
graphql_query = response.choices[0].message.content.strip() | ||
|
||
graphql_query = graphql_query.replace('```graphql', '').replace('```', '').strip() | ||
print(f"Generated GraphQL Query: {graphql_query}") # Debug: Print the generated GraphQL query | ||
|
||
return JsonResponse({'graphql_query': graphql_query}, status=200) | ||
|
||
except Exception as e: | ||
print(f"Error: {e}") # Debug: Print the error | ||
return JsonResponse({'error': str(e)}, status=500) |
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 @@ | ||
from django.shortcuts import render | ||
|
||
# Create your views here. |
Oops, something went wrong.