Skip to content

Commit

Permalink
Merge pull request #37 from nprasad2077/playoffs
Browse files Browse the repository at this point in the history
Playoffs
  • Loading branch information
nprasad2077 authored May 16, 2024
2 parents c5ac88e + 4bd0cb1 commit 187d1cb
Show file tree
Hide file tree
Showing 17 changed files with 1,055 additions and 2 deletions.
3 changes: 2 additions & 1 deletion docs/README3.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Access the API

To interact with the NBA database, use the API endpoint provided below:

- **Endpoint URL**: [http://209.38.172.107/graphql/](http://209.38.172.107/graphql/)

This endpoint allows you to use the GraphiQL interface for testing and querying data.
Expand Down Expand Up @@ -149,4 +150,4 @@ query {

For an exhaustive list of fields and detailed query examples, please consult the GraphQL schema documentation accessible through the [GraphiQL interface](http://209.38.172.107/graphql/).

This guide equips you with the necessary tools to explore and leverage the NBA_GraphQL API for your applications and research.
This guide equips you with the necessary tools to explore and leverage the NBA_GraphQL API for your applications and research.
Empty file.
3 changes: 3 additions & 0 deletions nba_graphql/graphql_input/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions nba_graphql/graphql_input/apps.py
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.
3 changes: 3 additions & 0 deletions nba_graphql/graphql_input/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
3 changes: 3 additions & 0 deletions nba_graphql/graphql_input/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
256 changes: 256 additions & 0 deletions nba_graphql/graphql_input/totals.py
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)
3 changes: 3 additions & 0 deletions nba_graphql/graphql_input/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
Loading

0 comments on commit 187d1cb

Please sign in to comment.