How to Build an AI Agent LinkedIn Post Generator in Java Spring Boot with ChatGPT

Random code · Beginner ·🔧 Backend Engineering ·1y ago

Key Takeaways

The video demonstrates how to build an AI agent in Java Spring Boot that generates LinkedIn posts using ChatGPT and OpenAI's API. It showcases a basic flow of creating a web socket, connecting to ChatGPT, and using a post generation service to create a LinkedIn post.

Full Transcript

Hey, it's random code here and today I want to show you guys a little AI agent I have created in spring boot with Java and very much just go through the basic flow. Not going to go too much into detail but then just give a basic idea and again have a link in description to get a repository where you can see how we can actually do all of this in Java. So the general idea is I'm creating a web socket allowing us to kind of make a responses back and forth. So we kind of have like this interactive agent which in this case I wanted to do something very specific. So I have created a system that kind of builds a LinkedIn post as I'm very fascinating with LinkedIn and like LinkedIn community building these days. But just general workflow we have a websocket which then set up on /vws/ chat. We then have a websocket handler which then kind of handle this flow. So when a websocket is established, it creates some like local variables keeping track of the user and the user progress. We then have a pressage just saying like hey this is what we're doing. Let's get started. And then each time a message is sent from the user, it then runs through this handle text message function or method if we're going back to Java and it then simply checks depending on I have some questions I'm going to be asking are we the questions when we through the questions write a post for LinkedIn do we then want to just continue or ream it and so on. So we have this workflow of input checking the input and what we're doing exactly is that each input have like a step. So we have like a step counter first counting if uh the step counter higher than the amount of questions we have. If it's higher than the amount of questions then we've been for the questions then we can prompt or create a post. Then we ask is it good or do you want it improved? Depending on the response we then go through this flow and we then have a few different helpers. One can say tools that this like base system can then utilize. For example, we have a post generation service, a post improvement service, a post rating service. So in the end, for example, when I then create a post, I also then resent the post using this prompt template to like get a rating of the post. But all of this sound a bit janky. So let's actually just showcase how it works. So I would connect. And one thing to keep in mind right now I'm using Postman to just connect directly to the websocket. So obviously if this were a real thing, we would have a proper UI to actually make it a bit more useful or user friendly. But now this is just a simple way of be able to use it very quickly. And note the messages are coming from the top down. So at first we're told let's create a LinkedIn post. And then the first question, what is the topic of your LinkedIn post? And just to make it a bit simpler, I'm just going to copy paste a few prompts to get a post about tech startups. So first, what is the topic? I will then paste tech startups. Then ask the next question. What person experience do you have? We're going to say that tech startups are harder than you might think. It then ask for key takeaways. So, we're going to say that as long as you keep going, you're going to get results at some point. Highlight some achievements again to get some more specific user input as I think what is very important in like an AI agent set up in any way thinking forward is kind of being able to utilize this like human input the most. So, I think this idea of having like an input or agent like this that ask questions first to then get a better understanding is kind of very useful. So, for example, we ask for some achievement of metrics. Let's say for example we had 100 users in the first month. Do we want a specific tone? Let's say we want this post to be casual friendly, maybe a bit funny. And then it's actually generating. And then after a few seconds, we should see here that we now have a post. Oh, here's your post. And I've already guided a bit that I don't want markdown setup emojis just plain text because that's is at least my experience the best performing on LinkedIn. And again we then get a post and we also get a rating. So it's kind of like re-evaluating its own input. And then we asked do you want to just post it which basically just ends this process or do you want to improve it? But let's actually first have a quick look at the at whatever is created. It is a bit hard in this format. So let's actually post it to a quick notepad. And we can see that it created a post saying startup. No, starting a tech startup is harder than you might think. Picture this silver lining. Sure. A few days blah blah blah. So it created a post and it rated it 8 out of 10. So let's say we now are actually not happy with this. We can tell it to improve. So again we're in this workflow of having multiple avenues of continuing. And in this case when we say improve it's going to then recreate the post. And in this case, it then either finds that it created a better post or it finds that its improvement didn't make it better. So it again it's going to be reanking itself and then like based on that re-ranking in this case it was was either I think eight or lower though it was not a better post. So now it's asking for more information. Let's add something like So just quickly added like I have been through the process of creating a startup of creating startup stuff and it can be hard but rewarding in the end. So let's see if we then actually pass this message and actually in this case it tried again to create a better post. It was not successful. So it kind of just uh ended its own loop. But again that's the general idea here. I'm actually well it doesn't matter. The point is not so much the post. is more the idea of this flow and this idea of using a web socket to then continuously take in inputs and then based on that input then be able to know again this is basically just an if else huge if else huge but an if else to determine which way we're going if I was actually creating a proper a bit more functional agent in Java I would probably replace all of this with some kind of like structure inspired by some of the Python libraries like long graph long chain by actually creating a bit more structured object-oriented flow. But in in short, this AI agent is just a simple if else flow that based on some inputs or again based on the step or the step count continues to improve or well at some point end the flow. And again the basic setup I have there's a bunch of stuff here which is more or less relevant but I think if you can just understand this basic flow and actually have a look at the code yourself it's going to be way more useful meaning I'll leave a link in description to GitHub and again you can have a look at all of it and also what is somewhat relevant is how we actually connect to the LM is I'm just using spring AI which is kind of like a library allowing us to simply just create a chat client and then And in this case, just saying we're using CHBT. And in my application properties, I have a private key allowing me to connect, which I'm obviously not going to be sharing, but you could add your own if you built this full well project. But anyway, that was just hopefully a somewhat quick brief overview of a agent in Java. And if you enjoyed this video, please leave a like and subscribe. And I wish you all a wonderful day.

Original Description

In this video, I give a quick overview of my thoughts on how to build an AI agent in Java Spring Boot using a web socket, connecting it to ChatGPT with OpenAI's API and Spring AI. GitHub: https://github.com/Da9el00/AI-Agent-LinkedIn-Post-Generator/blob/main/src/main/resources/application.properties Also, feel free to say hi on LinkedIn if you're a fellow LinkedIn enthusiast! https://www.linkedin.com/in/daniel-smidstrup-b98390226/ Welcome to my programming and software engineering channel! You'll find various videos on Java, Python, and Docker topics here. Whether you're a beginner looking to learn a new language or an experienced developer looking to stay up-to-date with the latest trends, my channel has something for everyone. Join me on this journey, and let's learn together! Don't forget to subscribe to stay up-to-date with all of my latest videos: https://www.youtube.com/channel/UCyjEXAHK8xfRmlnkat2ympQ?sub_confirmation=1
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

This video teaches how to build a basic AI agent in Java Spring Boot that generates LinkedIn posts using ChatGPT and OpenAI's API. It covers the basic flow of creating a web socket, connecting to ChatGPT, and using a post generation service.

Key Takeaways
  1. Create a new Java Spring Boot project
  2. Add the necessary dependencies for WebSocket and Spring AI
  3. Set up a WebSocket endpoint
  4. Connect to OpenAI's API using Spring AI
  5. Create a post generation service
  6. Use the post generation service to create a LinkedIn post
  7. Test the AI agent using Postman
💡 The video showcases how to use a web socket to continuously take in inputs and improve the generated LinkedIn post based on user feedback.

Related Reads

Up next
/dev/push: An Open Vercel Alternative to Ship Your Apps Quickly
Ian Wootten
Watch →