Integrating ChatGPT API with My Side Project: A Step-by-Step Guide

JackyNote ⭐️
2 min readMar 2, 2024

About the Project

This side project aims to explore the integration of ChatGPT API into a web application. The primary purpose is to facilitate the generation of cover letters based on uploaded PDF resumes.

Project Workflow

  1. User Uploads PDF: Users visit the website and upload their resumes in PDF format.
  2. PDF Processing: The backend processes the uploaded PDF, extracting the text content.
  3. Integration with ChatGPT API: A prompt is constructed using the extracted resume content, and ChatGPT API is utilized to generate a cover letter.

Tech Stack

  • Backend: Groovy 3+ with Spring Boot
  • Frontend: Pebble template engine

Step-by-Step Implementation

Step 1: Setting Up the Project

Start by initializing a Spring Boot project and importing necessary dependencies. Ensure you have the required libraries in your build.gradle file:

dependencies {
implementation 'org.codehaus.groovy:groovy-all:3.0.19'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'io.pebbletemplates:pebble-spring-boot-starter:3.2.1'
implementation group: 'io.github.http-builder-ng', name: 'http-builder-ng-core', version: '1.0.4'
implementation 'org.apache.pdfbox:pdfbox:3.0.0'
// Add any additional dependencies here
}

Step 2: Handling PDF Upload and Extraction

Use MultipartFile to handle file uploads and extract text from the PDF file:

PDDocument document = Loader.loadPDF(file.getBytes())
PDFTextStripper pdfTextStripper = new PDFTextStripper()
String text = pdfTextStripper.getText(document)
document.close()

Step 3: Creating a Prompt for ChatGPT API

Refer to the official documentation of ChatGPT API for integration details: https://platform.openai.com/docs/api-reference/chat

Construct a prompt using the extracted resume content:

def messages = new ArrayList<Map>()
messages.add(["role": "user", "content": "This is my resume: ${text}"])
messages.add(["role": "user", "content": "Generate a cover letter based on the above resume"])
def result = openAIProxy.completions(messages)

Step 4: Integrating with ChatGPT API

Implement the integration with ChatGPT API using the provided endpoint:

Map completions(List<Map> userMessages) {
def url = "https://api.openai.com/v1/chat/completions"
def messages = new ArrayList()
messages.add([
"role": "assistant",
"content": "You act as a personal financial advisor for users who can spend wisely. Users will provide personal financial information to you from json file."
])

messages.addAll(userMessages)
def payload = [
"model": "gpt-3.5-turbo-1106",
"temperature": 1,
"stream": false,
"n": 1,
"top_p": 1,
"presence_penalty": 0,
"frequency_penalty": 0,
"messages": messages
]
def request = new HttpEntity<>(JsonOutput.toJson(payload), baseHeaders())
return restTemplate.postForObject(url, request, Map.class)["choices"][0]
}

Conclusion

Integrating ChatGPT API into your project opens up exciting possibilities for automating tasks such as cover letter generation. By following these steps, you can seamlessly incorporate AI-powered features into your application, enhancing its functionality and user experience.

Happy coding!

--

--

JackyNote ⭐️

🚀 Software Engineer | Full Stack Java 7 Years of Experience | Tech Enthusiast | Startup Lover | Founder of helik.co - URL Shorten | Coffee