Skip to content

Conversation

@AkhileshNegi
Copy link
Collaborator

@AkhileshNegi AkhileshNegi commented Mar 13, 2025

Summary

Target issue is #14

Checklist

Before submitting a pull request, please ensure that you mark these task.

  • Ran fastapi run --reload app/main.py in the repository root and test.
  • If you've fixed a bug or added code that is tested and has test cases.

Notes

  • Added new endpoint to run OpenAI threads
  • Added testcases

This pull request introduces several enhancements and new features to the staging branch of the ProjectTech4DevAI/ai-platform repository. The main focus is on integrating OpenAI thread handling capabilities into the FastAPI application. Key changes include:

  1. Thread Router Integration: The main.py file in the API module has been updated to include a thread router, improving the organization of imports and formatting.

  2. New Endpoint for OpenAI Threads: A new FastAPI endpoint is added in routes/threads.py to manage OpenAI assistant threads. This includes functionality for thread creation, message handling, and asynchronous processing of OpenAI responses, along with background processing and callback support.

  3. Core Module Initialization: Basic initialization code for core module components is introduced in the __init__.py file of the core module.

  4. Configuration Enhancements: The config.py file now includes configuration for the OPENAI_API_KEY and handling for the LOG_DIR path.

  5. Logging Setup: A logging configuration setup is implemented in logger.py, featuring rotating file handlers and basic logging configuration to enhance logging capabilities.

@AkhileshNegi AkhileshNegi added the enhancement New feature or request label Mar 13, 2025
@AkhileshNegi AkhileshNegi self-assigned this Mar 13, 2025
@AkhileshNegi AkhileshNegi linked an issue Mar 13, 2025 that may be closed by this pull request
@AkhileshNegi AkhileshNegi force-pushed the feature/open-ai-threads branch from be30e41 to 00ebe6f Compare March 17, 2025 05:10
Copy link
Contributor

@sourabhlodha sourabhlodha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please check response format

@@ -0,0 +1,133 @@
import pytest
import openai
from unittest.mock import MagicMock, patch
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standard library imports should come first. See PEP8

Once completed, calls send_callback with the final result.
"""
client = OpenAI(api_key=settings.OPENAI_API_KEY)
assistant_error = validate_assistant_id(request["assistant_id"], client)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider avoiding this test in favor of client.beta.threads.runs.create_and_poll reporting the error

Comment on lines +105 to +114
try:
runs = client.beta.threads.runs.list(thread_id=thread_id)
# Get the most recent run (first in the list) if any
if runs.data and len(runs.data) > 0:
latest_run = runs.data[0]
if latest_run.status in ["queued", "in_progress", "requires_action"]:
return APIResponse.failure_response(error=f"There is an active run on this thread (status: {latest_run.status}). Please wait for it to complete.")
except openai.OpenAIError:
# Handle invalid thread ID
return APIResponse.failure_response(error=f"Invalid thread ID provided {thread_id}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider avoiding this test in favor of client.beta.threads.runs.create_and_poll reporting the error

@AkhileshNegi AkhileshNegi linked an issue Mar 24, 2025 that may be closed by this pull request
@kody-ai
Copy link

kody-ai bot commented Mar 26, 2025

Code Review Completed! 🔥

The code review was successfully completed based on your current configurations.

Kody Guide: Usage and Configuration
Interacting with Kody
  • Request a Review: Ask Kody to review your PR manually by adding a comment with the @kody start-review command at the root of your PR.

  • Provide Feedback: Help Kody learn and improve by reacting to its comments with a 👍 for helpful suggestions or a 👎 if improvements are needed.

Current Kody Configuration
Review Options

The following review options are enabled or disabled:

Options Enabled
Security
Code Style
Kody Rules
Refactoring
Error Handling
Maintainability
Potential Issues
Documentation And Comments
Performance And Optimization
Breaking Changes

Access your configuration settings here.

try:
session = requests.Session()
# uncomment this to run locally without SSL
session.verify = False
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kody code-review Security high

        session.verify = settings.VERIFY_SSL

The session.verify = False line disables SSL verification, which is a security risk. This should be configurable via environment variables rather than hardcoded.

Talk to Kody by mentioning @kody

Was this suggestion helpful? React with 👍 or 👎 to help Kody learn from this interaction.

latest_message = messages.data[0]
message_content = latest_message.content[0].text.value

if request["remove_citation"]:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kody code-review Error Handling high

            if request.get('remove_citation', False):

The function should handle KeyError exceptions when accessing dictionary keys. Currently, direct access to request['remove_citation'] could raise KeyError if the key is missing.

Talk to Kody by mentioning @kody

Was this suggestion helpful? React with 👍 or 👎 to help Kody learn from this interaction.

Comment on lines +7 to +8
if not os.path.exists(LOG_DIR):
os.makedirs(LOG_DIR)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kody code-review Error Handling high

try:
    if not os.path.exists(LOG_DIR):
        os.makedirs(LOG_DIR)
except (OSError, IOError) as e:
    import sys
    sys.stderr.write(f'Failed to create log directory: {e}\n')
    raise

Add error handling around file operations to gracefully handle permission issues or disk space problems when creating log directory and files

Talk to Kody by mentioning @kody

Was this suggestion helpful? React with 👍 or 👎 to help Kody learn from this interaction.

Comment on lines +15 to +22
logging.basicConfig(level=LOGGING_LEVEL, format=LOGGING_FORMAT)

file_handler = RotatingFileHandler(
LOG_FILE_PATH, maxBytes=10485760, backupCount=5)
file_handler.setLevel(LOGGING_LEVEL)
file_handler.setFormatter(logging.Formatter(LOGGING_FORMAT))

logging.getLogger("").addHandler(file_handler)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kody code-review Maintainability medium

def setup_logging():
    logging.basicConfig(level=LOGGING_LEVEL, format=LOGGING_FORMAT)
    
    file_handler = RotatingFileHandler(
        LOG_FILE_PATH, maxBytes=10485760, backupCount=5)
    file_handler.setLevel(LOGGING_LEVEL)
    file_handler.setFormatter(logging.Formatter(LOGGING_FORMAT))
    
    logging.getLogger("").addHandler(file_handler)

setup_logging()

Move logging configuration into a function to ensure proper initialization and allow for potential reconfiguration

Talk to Kody by mentioning @kody

Was this suggestion helpful? React with 👍 or 👎 to help Kody learn from this interaction.

@kody-ai
Copy link

kody-ai bot commented Mar 26, 2025

Kody Review Complete

Great news! 🎉
No issues were found that match your current review configurations.

Keep up the excellent work! 🚀

Kody Guide: Usage and Configuration
Interacting with Kody
  • Request a Review: Ask Kody to review your PR manually by adding a comment with the @kody start-review command at the root of your PR.

  • Provide Feedback: Help Kody learn and improve by reacting to its comments with a 👍 for helpful suggestions or a 👎 if improvements are needed.

Current Kody Configuration
Review Options

The following review options are enabled or disabled:

Options Enabled
Security
Code Style
Kody Rules
Refactoring
Error Handling
Maintainability
Potential Issues
Documentation And Comments
Performance And Optimization
Breaking Changes

Access your configuration settings here.

@kody-ai
Copy link

kody-ai bot commented Mar 26, 2025

Kody Review Complete

Great news! 🎉
No issues were found that match your current review configurations.

Keep up the excellent work! 🚀

Kody Guide: Usage and Configuration
Interacting with Kody
  • Request a Review: Ask Kody to review your PR manually by adding a comment with the @kody start-review command at the root of your PR.

  • Provide Feedback: Help Kody learn and improve by reacting to its comments with a 👍 for helpful suggestions or a 👎 if improvements are needed.

Current Kody Configuration
Review Options

The following review options are enabled or disabled:

Options Enabled
Security
Code Style
Kody Rules
Refactoring
Error Handling
Maintainability
Potential Issues
Documentation And Comments
Performance And Optimization
Breaking Changes

Access your configuration settings here.

@sourabhlodha sourabhlodha merged commit 67b89c8 into staging Mar 26, 2025
1 check failed
@sourabhlodha sourabhlodha deleted the feature/open-ai-threads branch March 26, 2025 05:07
sourabhlodha added a commit that referenced this pull request Mar 26, 2025
* Update README.md (#44)

* changes (#45)

Co-authored-by: sourabhlodha <sourabhlodha@Administrators-MacBook-Pro.local>

* Readme update (#47)

rename project and stack

---------

Co-authored-by: sourabhlodha <sourabhlodha@Administrators-MacBook-Pro.local>

* fix create_user endpoint (#62)

* standard api response and http exception handling (#67)

* Upgrade PostgreSQL to 16 & Fix CORS Configuration (#57)

* use latest docker image

* update envsample

* Add Customizable Token Expiry Time in Login API (#70)

* token expiry time can be customize

* default to one day

* Organization/project : Crud, Endpoint and Test Cases (#63)

* trial

* pushing all

* models file

* renaming

* Rename Project.py to project.py

* Rename oganization.py to organization.py

* Update README.md (#44)

* changes (#45)

Co-authored-by: sourabhlodha <sourabhlodha@Administrators-MacBook-Pro.local>

* Readme update (#47)

rename project and stack

---------

Co-authored-by: sourabhlodha <sourabhlodha@Administrators-MacBook-Pro.local>

* fix create_user endpoint (#62)

* standard api response and http exception handling (#67)

* standardization and edits

* small edits

* small edits

* small edits

* fixed project post

* trial

* pushing all

* models file

* renaming

* Rename Project.py to project.py

* Rename oganization.py to organization.py

* standardization and edits

* small edits

* small edits

* small edits

* fixed project post

* remove these files since they were somehow pushed into this branch

* re-push the docker file

* re-push utils file

* re-push the file

* fixing test cases

---------

Co-authored-by: Sourabh Lodha <sourabh_lodha@ymail.com>
Co-authored-by: sourabhlodha <sourabhlodha@Administrators-MacBook-Pro.local>
Co-authored-by: Aviraj Gour <100823015+avirajsingh7@users.noreply.github.com>
Co-authored-by: Ishankoradia <ikoradia@umich.edu>

* Add Project User Management (#65)

* intial commit user project mapping and authorization

* fix alembic migration

* Use standard API response

* add pagination

* add index and use base model

* Alembic: migration fixes for organization  (#77)

* fixing testcases and migrations

* changes migration file name

* remove old migration

---------

Co-authored-by: Akhilesh Negi <akhileshnegi.an3@gmail.com>

* Added Support of API Key Authentication (#76)

* Intial setup api key

* added Api key auth flow

* support both api key and oauth

---------

Co-authored-by: Sourabh Lodha <sourabh_lodha@ymail.com>

* Main to stage code sync (#80)

Back merge Production to staging code

* added migration for api table (#81)

* Refactor Authentication Logic and Testing Enhancements (#89)

* fix authentication part

* Modify test cases to compatible with new auth

* Github: CI (#74)

* issue CI

* first stab at continuous integration

* fixing testcases and migrations

* syncing with master

* moving to python version 3.11.7

* making copy of env

* updating env

* added migrations

* added uv sync

* updating working directory

* added step to activate env

* updating working directory

* updating working directory for codecov upload

* updating script to upload to codecov

* remove working directory

* added working directory for % check

* clenaup

* cleanup

* activating env

* update the issue template

* update readme and env file

* adding badges (#91)

* OpenAI: Threads (#40)

* getting threads up and running

* added testcases and citation

* removing ssl verify

* using standardized APIResponse

* getting rid of redundant files

* refactor code after testing

* refactor testcases

* setting up init.py

* fixing review comments

* cleanup

* cleanup

* removed validate thread as it can be handled by default

* fixing few code review suggestions

* removed validation testcases for assistant ID

* threads testcases fix (#93)

* updating CI

---------

Co-authored-by: Sourabh Lodha <sourabh_lodha@ymail.com>
Co-authored-by: sourabhlodha <sourabhlodha@Administrators-MacBook-Pro.local>
Co-authored-by: Aviraj Gour <100823015+avirajsingh7@users.noreply.github.com>
Co-authored-by: Nishika Yadav <89646695+nishika26@users.noreply.github.com>
Co-authored-by: Ishankoradia <ikoradia@umich.edu>
AkhileshNegi added a commit that referenced this pull request Apr 17, 2025
* Update README.md (#44)

* changes (#45)

* Readme update (#47)

rename project and stack

---------

Co-authored-by: sourabhlodha <sourabhlodha@Administrators-MacBook-Pro.local>

* fix create_user endpoint (#62)

* standard api response and http exception handling (#67)

* Upgrade PostgreSQL to 16 & Fix CORS Configuration (#57)

* use latest docker image

* update envsample

* Add Customizable Token Expiry Time in Login API (#70)

* token expiry time can be customize

* default to one day

* Organization/project : Crud, Endpoint and Test Cases (#63)

* trial

* pushing all

* models file

* renaming

* Rename Project.py to project.py

* Rename oganization.py to organization.py

* Update README.md (#44)

* changes (#45)

Co-authored-by: sourabhlodha <sourabhlodha@Administrators-MacBook-Pro.local>

* Readme update (#47)

rename project and stack

---------

Co-authored-by: sourabhlodha <sourabhlodha@Administrators-MacBook-Pro.local>

* fix create_user endpoint (#62)

* standard api response and http exception handling (#67)

* standardization and edits

* small edits

* small edits

* small edits

* fixed project post

* trial

* pushing all

* models file

* renaming

* Rename Project.py to project.py

* Rename oganization.py to organization.py

* standardization and edits

* small edits

* small edits

* small edits

* fixed project post

* remove these files since they were somehow pushed into this branch

* re-push the docker file

* re-push utils file

* re-push the file

* fixing test cases

---------

Co-authored-by: Sourabh Lodha <sourabh_lodha@ymail.com>
Co-authored-by: sourabhlodha <sourabhlodha@Administrators-MacBook-Pro.local>
Co-authored-by: Aviraj Gour <100823015+avirajsingh7@users.noreply.github.com>
Co-authored-by: Ishankoradia <ikoradia@umich.edu>

* Add Project User Management (#65)

* intial commit user project mapping and authorization

* fix alembic migration

* Use standard API response

* add pagination

* add index and use base model

* Alembic: migration fixes for organization  (#77)

* fixing testcases and migrations

* changes migration file name

* remove old migration

---------

Co-authored-by: Akhilesh Negi <akhileshnegi.an3@gmail.com>

* Added Support of API Key Authentication (#76)

* Intial setup api key

* added Api key auth flow

* support both api key and oauth

---------

Co-authored-by: Sourabh Lodha <sourabh_lodha@ymail.com>

* Main to stage code sync (#80)

Back merge Production to staging code

* added migration for api table (#81)

* creds table

* Refactor Authentication Logic and Testing Enhancements (#89)

* fix authentication part

* Modify test cases to compatible with new auth

* Github: CI (#74)

* issue CI

* first stab at continuous integration

* fixing testcases and migrations

* syncing with master

* moving to python version 3.11.7

* making copy of env

* updating env

* added migrations

* added uv sync

* updating working directory

* added step to activate env

* updating working directory

* updating working directory for codecov upload

* updating script to upload to codecov

* remove working directory

* added working directory for % check

* clenaup

* cleanup

* activating env

* update the issue template

* update readme and env file

* adding badges (#91)

* OpenAI: Threads (#40)

* getting threads up and running

* added testcases and citation

* removing ssl verify

* using standardized APIResponse

* getting rid of redundant files

* refactor code after testing

* refactor testcases

* setting up init.py

* fixing review comments

* cleanup

* cleanup

* removed validate thread as it can be handled by default

* fixing few code review suggestions

* removed validation testcases for assistant ID

* threads testcases fix (#93)

* project router changes

* endpoint,crud and migration file

* models file

* minor fix

* fixes

* fixes

* test cases and fixes

* alembic file

* type checking

* cleaner exception

* fixing alembic revision heads

* using crendentials

* init module

* running pre commit

* running pre commit

* final changes

* migration file

* Rename fa868aa8debd_add_credetial_table.py to fa868aa8debd_add_credential_table.py

* test case change

* test cases

* test cases

* test cases

* removing duplicate lines

* datetime columns addition

* migration file

---------

Co-authored-by: Sourabh Lodha <sourabh_lodha@ymail.com>
Co-authored-by: Aviraj Gour <100823015+avirajsingh7@users.noreply.github.com>
Co-authored-by: Akhilesh Negi <akhileshnegi.an3@gmail.com>
priyanshu6238 pushed a commit to priyanshu6238/ai-platform that referenced this pull request Apr 23, 2025
* Update README.md (ProjectTech4DevAI#44)

* changes (ProjectTech4DevAI#45)

* Readme update (ProjectTech4DevAI#47)

rename project and stack

---------

Co-authored-by: sourabhlodha <sourabhlodha@Administrators-MacBook-Pro.local>

* fix create_user endpoint (ProjectTech4DevAI#62)

* standard api response and http exception handling (ProjectTech4DevAI#67)

* Upgrade PostgreSQL to 16 & Fix CORS Configuration (ProjectTech4DevAI#57)

* use latest docker image

* update envsample

* Add Customizable Token Expiry Time in Login API (ProjectTech4DevAI#70)

* token expiry time can be customize

* default to one day

* Organization/project : Crud, Endpoint and Test Cases (ProjectTech4DevAI#63)

* trial

* pushing all

* models file

* renaming

* Rename Project.py to project.py

* Rename oganization.py to organization.py

* Update README.md (ProjectTech4DevAI#44)

* changes (ProjectTech4DevAI#45)

Co-authored-by: sourabhlodha <sourabhlodha@Administrators-MacBook-Pro.local>

* Readme update (ProjectTech4DevAI#47)

rename project and stack

---------

Co-authored-by: sourabhlodha <sourabhlodha@Administrators-MacBook-Pro.local>

* fix create_user endpoint (ProjectTech4DevAI#62)

* standard api response and http exception handling (ProjectTech4DevAI#67)

* standardization and edits

* small edits

* small edits

* small edits

* fixed project post

* trial

* pushing all

* models file

* renaming

* Rename Project.py to project.py

* Rename oganization.py to organization.py

* standardization and edits

* small edits

* small edits

* small edits

* fixed project post

* remove these files since they were somehow pushed into this branch

* re-push the docker file

* re-push utils file

* re-push the file

* fixing test cases

---------

Co-authored-by: Sourabh Lodha <sourabh_lodha@ymail.com>
Co-authored-by: sourabhlodha <sourabhlodha@Administrators-MacBook-Pro.local>
Co-authored-by: Aviraj Gour <100823015+avirajsingh7@users.noreply.github.com>
Co-authored-by: Ishankoradia <ikoradia@umich.edu>

* Add Project User Management (ProjectTech4DevAI#65)

* intial commit user project mapping and authorization

* fix alembic migration

* Use standard API response

* add pagination

* add index and use base model

* Alembic: migration fixes for organization  (ProjectTech4DevAI#77)

* fixing testcases and migrations

* changes migration file name

* remove old migration

---------

Co-authored-by: Akhilesh Negi <akhileshnegi.an3@gmail.com>

* Added Support of API Key Authentication (ProjectTech4DevAI#76)

* Intial setup api key

* added Api key auth flow

* support both api key and oauth

---------

Co-authored-by: Sourabh Lodha <sourabh_lodha@ymail.com>

* Main to stage code sync (ProjectTech4DevAI#80)

Back merge Production to staging code

* added migration for api table (ProjectTech4DevAI#81)

* creds table

* Refactor Authentication Logic and Testing Enhancements (ProjectTech4DevAI#89)

* fix authentication part

* Modify test cases to compatible with new auth

* Github: CI (ProjectTech4DevAI#74)

* issue CI

* first stab at continuous integration

* fixing testcases and migrations

* syncing with master

* moving to python version 3.11.7

* making copy of env

* updating env

* added migrations

* added uv sync

* updating working directory

* added step to activate env

* updating working directory

* updating working directory for codecov upload

* updating script to upload to codecov

* remove working directory

* added working directory for % check

* clenaup

* cleanup

* activating env

* update the issue template

* update readme and env file

* adding badges (ProjectTech4DevAI#91)

* OpenAI: Threads (ProjectTech4DevAI#40)

* getting threads up and running

* added testcases and citation

* removing ssl verify

* using standardized APIResponse

* getting rid of redundant files

* refactor code after testing

* refactor testcases

* setting up init.py

* fixing review comments

* cleanup

* cleanup

* removed validate thread as it can be handled by default

* fixing few code review suggestions

* removed validation testcases for assistant ID

* threads testcases fix (ProjectTech4DevAI#93)

* project router changes

* endpoint,crud and migration file

* models file

* minor fix

* fixes

* fixes

* test cases and fixes

* alembic file

* type checking

* cleaner exception

* fixing alembic revision heads

* using crendentials

* init module

* running pre commit

* running pre commit

* final changes

* migration file

* Rename fa868aa8debd_add_credetial_table.py to fa868aa8debd_add_credential_table.py

* test case change

* test cases

* test cases

* test cases

* removing duplicate lines

* datetime columns addition

* migration file

---------

Co-authored-by: Sourabh Lodha <sourabh_lodha@ymail.com>
Co-authored-by: Aviraj Gour <100823015+avirajsingh7@users.noreply.github.com>
Co-authored-by: Akhilesh Negi <akhileshnegi.an3@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Core: Logger OpenAI: Threads Module

4 participants