From 848cd514d00722c54daa6674b02df6b7e0b0c99a Mon Sep 17 00:00:00 2001 From: Sphericalkat Date: Sat, 27 May 2023 13:34:41 +0530 Subject: [PATCH] feat: add sample response.json for dev Signed-off-by: Sphericalkat --- api/routes/show.go | 17 +++++++++++++++-- pkg/client/medium_client.go | 20 +++++++++++++++++++- response.json | 1 + 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 response.json diff --git a/api/routes/show.go b/api/routes/show.go index 8baee35..400e71f 100644 --- a/api/routes/show.go +++ b/api/routes/show.go @@ -1,9 +1,22 @@ package routes -import "github.com/gofiber/fiber/v2" +import ( + "github.com/gofiber/fiber/v2" + "github.com/medium.rip/pkg/client" +) func show(c *fiber.Ctx) error { - return nil + postId := c.Params("id", "") + if postId == "" { + return c.Redirect("/") + } + + e, err := client.PostData(postId) + if err != nil { + return err + } + + return c.JSON(e) } func index(c *fiber.Ctx) error { diff --git a/pkg/client/medium_client.go b/pkg/client/medium_client.go index 55aca1b..a43d827 100644 --- a/pkg/client/medium_client.go +++ b/pkg/client/medium_client.go @@ -2,15 +2,33 @@ package client import ( "fmt" - log "github.com/sirupsen/logrus" "io" "net/http" + "os" "strings" + log "github.com/sirupsen/logrus" + + "github.com/medium.rip/internal/config" "github.com/medium.rip/pkg/entities" ) func PostData(postId string) (*entities.MediumResponse, error) { + if config.Conf.Env == "dev" { + file, err := os.ReadFile("response.json") + if err != nil { + return nil, err + } + + mr, err := entities.UnmarshalMediumResponse(file) + if err != nil { + log.Printf("Error unmarshalling body from response %v\n", err) + return nil, err + } + + return &mr, nil + } + // http client to post data url := "https://medium.com/_/graphql" method := "POST" diff --git a/response.json b/response.json new file mode 100644 index 0000000..3f7f065 --- /dev/null +++ b/response.json @@ -0,0 +1 @@ +{"data":{"post":{"title":"Pandas AI — The Future of Data Analysis","createdAt":1683185501743,"creator":{"id":"b856005e5ecd","name":"Fareed Khan"},"content":{"bodyModel":{"paragraphs":[{"name":"b238","text":"Pandas AI — The Future of Data Analysis","type":"H3","href":null,"layout":null,"markups":[],"iframe":null,"metadata":null},{"name":"cbfd","text":"","type":"IMG","href":null,"layout":"INSET_CENTER","markups":[],"iframe":null,"metadata":{"id":"1*awdsJFX9yyTGK7Ym9I2TtA.png","originalWidth":1481,"originalHeight":758}},{"name":"2c3a","text":"Imagine being able to talk to your data like it’s your best friend. That’s what Pandas AI does! This Python library has generative artificial intelligence capabilities that can turn your dataframes into conversationalists. No more endless hours of staring at rows and columns.","type":"P","href":null,"layout":null,"markups":[],"iframe":null,"metadata":null},{"name":"a1ee","text":"But don’t worry, Pandas AI is not here to replace your beloved Pandas. It’s here to enhance it! With Pandas AI, you can take your data analysis and manipulation to the next level. Think of it like a superhero sidekick — it’s there to help you save the day and make your life easier.","type":"P","href":null,"layout":null,"markups":[],"iframe":null,"metadata":null},{"name":"5514","text":"The possibilities with Pandas AI are endless. Imagine having a dataframe that can write its own reports, or one that can analyze complex data and provide you with easy-to-understand summaries.","type":"P","href":null,"layout":null,"markups":[],"iframe":null,"metadata":null},{"name":"5171","text":"In this quick guide, you’ll get a step-by-step walkthrough of how to use this cutting-edge library, regardless of your level of experience in the field.","type":"P","href":null,"layout":null,"markups":[],"iframe":null,"metadata":null},{"name":"ab65","text":"Whether you’re an experienced data analyst or a beginner, this guide will equip you with all the tools you need to dive into the world of Pandas AI with confidence. So sit back, relax, and let’s explore the exciting possibilities that Pandas AI has to offer!","type":"P","href":null,"layout":null,"markups":[],"iframe":null,"metadata":null},{"name":"42f1","text":"Official GitHub Repository — https://github.com/gventuri/pandas-ai","type":"P","href":null,"layout":null,"markups":[{"title":"","type":"A","href":"https://github.com/gventuri/pandas-ai","userId":null,"start":29,"end":66,"anchorType":"LINK"},{"title":null,"type":"STRONG","href":null,"userId":null,"start":0,"end":28,"anchorType":null}],"iframe":null,"metadata":null},{"name":"7b4c","text":"Code— https://colab.research.google.com/drive/1rKz7TudOeCeKGHekw7JFNL4sagN9hon-?usp=sharing","type":"P","href":null,"layout":null,"markups":[{"title":"","type":"A","href":"https://colab.research.google.com/drive/1rKz7TudOeCeKGHekw7JFNL4sagN9hon-?usp=sharing","userId":null,"start":6,"end":91,"anchorType":"LINK"},{"title":null,"type":"STRONG","href":null,"userId":null,"start":0,"end":4,"anchorType":null}],"iframe":null,"metadata":null},{"name":"ae7c","text":"Installing Pandas AI using pip","type":"H4","href":null,"layout":null,"markups":[],"iframe":null,"metadata":null},{"name":"51fe","text":"pip install pandasai","type":"PRE","href":null,"layout":null,"markups":[],"iframe":null,"metadata":null},{"name":"4f69","text":"Our DataFrame contains information about various countries, including their GDP (in millions of USD) and happiness index scores. It consists of 10 rows and 3 columns:","type":"P","href":null,"layout":null,"markups":[],"iframe":null,"metadata":null},{"name":"4b06","text":"","type":"IMG","href":null,"layout":"INSET_CENTER","markups":[],"iframe":null,"metadata":{"id":"1*FvevlIS-mnRSvqg2Nq5JnQ.png","originalWidth":925,"originalHeight":809}},{"name":"29ef","text":"Importing PandasAI with OpenAI","type":"H4","href":null,"layout":null,"markups":[],"iframe":null,"metadata":null},{"name":"2656","text":"In the next step, we’ll import the pandasai library that we installed earlier and then import the LLM (Large Language Model) feature. As of May 2023, pandasai only supports the OpenAI model, which we’ll be utilizing understand the data.","type":"P","href":null,"layout":null,"markups":[],"iframe":null,"metadata":null},{"name":"a57a","text":"","type":"IMG","href":null,"layout":"INSET_CENTER","markups":[],"iframe":null,"metadata":{"id":"1*RsVA6lJngSkpSs_BB9yZAg.png","originalWidth":939,"originalHeight":409}},{"name":"e33e","text":"To use the OpenAI API, you must generate your own unique API key. If you haven’t done so already, you can easily create an account on the platform’s official website at platform.openai.com. Once you’ve created your account, you’ll receive an instant $5 credit that can be used to explore and experiment with the API.","type":"P","href":null,"layout":null,"markups":[],"iframe":null,"metadata":null},{"name":"a3ab","text":"Initializing PandasAI and asking Question","type":"H4","href":null,"layout":null,"markups":[],"iframe":null,"metadata":null},{"name":"52ae","text":"Afterwards, we’ll provide our OpenAI model to Pandas AI and ask various questions.","type":"P","href":null,"layout":null,"markups":[],"iframe":null,"metadata":null},{"name":"37d9","text":"","type":"IMG","href":null,"layout":"INSET_CENTER","markups":[],"iframe":null,"metadata":{"id":"1*UtA6DMSuzKtKOX5mNyfhCA.png","originalWidth":940,"originalHeight":226}},{"name":"7578","text":"When using pandas_ai.run, two parameters are necessary: the dataframe you’re working with and the question you’re seeking an answer to, it returns the top 5 happiest countries based on the supplied dataframe.","type":"P","href":null,"layout":null,"markups":[],"iframe":null,"metadata":null},{"name":"8f1b","text":"Asking Complex Questions","type":"H4","href":null,"layout":null,"markups":[],"iframe":null,"metadata":null},{"name":"6388","text":"Let’s check whether it can draw the plots for us?","type":"P","href":null,"layout":null,"markups":[],"iframe":null,"metadata":null},{"name":"1495","text":"","type":"IMG","href":null,"layout":"INSET_CENTER","markups":[],"iframe":null,"metadata":{"id":"1*0mLGIH_2j4K0OC9SZzNrZQ.png","originalWidth":940,"originalHeight":153}},{"name":"140d","text":"Yes it does plot the graph, based on the question I asked.","type":"P","href":null,"layout":null,"markups":[],"iframe":null,"metadata":null},{"name":"a242","text":"","type":"IMG","href":null,"layout":"INSET_CENTER","markups":[],"iframe":null,"metadata":{"id":"1*zxjcTNyFUFSn2g9aeLrImw.png","originalWidth":900,"originalHeight":863}},{"name":"d6ec","text":"Let’s perform a complex task, removing NAN values from the below dataset:","type":"P","href":null,"layout":null,"markups":[],"iframe":null,"metadata":null},{"name":"48b0","text":"","type":"IMG","href":null,"layout":"INSET_CENTER","markups":[],"iframe":null,"metadata":{"id":"1*5wd-jewNp91MNc69DPDe8Q.png","originalWidth":503,"originalHeight":481}},{"name":"c589","text":"This is the output we get:","type":"P","href":null,"layout":null,"markups":[],"iframe":null,"metadata":null},{"name":"a830","text":"","type":"IMG","href":null,"layout":"INSET_CENTER","markups":[],"iframe":null,"metadata":{"id":"1*VAEUvjecOWIz5JLtMKLCDg.png","originalWidth":940,"originalHeight":137}},{"name":"b01d","text":"But when I print the df variable again, it does remove those NAN values from the dataset, removing that row entirely","type":"P","href":null,"layout":null,"markups":[{"title":null,"type":"STRONG","href":null,"userId":null,"start":21,"end":23,"anchorType":null}],"iframe":null,"metadata":null},{"name":"9b78","text":"","type":"IMG","href":null,"layout":"INSET_CENTER","markups":[],"iframe":null,"metadata":{"id":"1*vomhvRkNY_B4v3M7bhoXJA.png","originalWidth":496,"originalHeight":400}},{"name":"f25e","text":"The pandasai library offers an extensive range of possibilities, and you can explore them all by visiting their official repository page, which I’ve shared earlier.","type":"P","href":null,"layout":null,"markups":[],"iframe":null,"metadata":null},{"name":"6d46","text":"It’s important to note that working with pandasai involves OpenAI pricing, and you can find the most up-to-date pricing information on their website. As of May 2023, the pricing is approximately 1000 tokens per $0.0200 (for the GPT-3.5-Turbo Model). When posing a question, it’s crucial to remember that the entire dataframe is passed along with the question every time, so it may not be an ideal solution for handling large datasets.","type":"H4","href":null,"layout":null,"markups":[],"iframe":null,"metadata":null},{"name":"67b7","text":"If you have any query feel free to ask me!","type":"H3","href":null,"layout":null,"markups":[],"iframe":null,"metadata":null}]}}}}}