Analyzing Articles in Collaboration with ChatGPT
In the realm of article analysis, numerous API providers cater to a wide range of article types. For the purpose of this article, we showcase the utilization of the esteemed https://newsapi.org/ API. Our appreciation goes out to their remarkable service, although it is worth noting that alternative providers could have been equally employed.
The primary objective of this article is to underscore the significance of leveraging an API service to access articles, thereby enabling the dissemination of articles on diverse topics to your esteemed audience. These topics can encompass a variety of choices, including both freely chosen subjects and those specifically tailored to your products, services, or related interests.
When presenting the article's title, author, URL, and description, it becomes highly valuable to offer an analysis of the subject matter prior to delving into the core content of the article itself. This is precisely where ChatGPT emerges as an indispensable tool, offering its invaluable services.
In order to optimize costs for both ChatGPT and the news service, a prudent approach involves running the analysis once a day and updating the content accordingly. Thus, the news API is utilized only once a day, while ChatGPT performs its insightful analysis on select articles within the same timeframe.
Given that the number of articles for analysis could potentially surpass 300, it is judicious to focus the analysis on a subset of articles. Selecting the first 10, 50, 100, or all articles, if cost permits, ensures efficient resource allocation.
To store the articles and their corresponding summaries for the day, an Azure Storage Container and files serve as the optimal solution. Upon program execution, the system first checks if it is a new day. If affirmative, it proceeds to execute the program afresh, obtaining the latest articles and generating ChatGPT's analysis for a predetermined number of articles.
To facilitate the reading of articles, an Azure MVC Web App has been developed, featuring a user-friendly page and menu. While articles covering ChatGPT, AI, and Google Bard domains have been prepared, this example solely focuses on articles related to ChatGPT.
Towards the end of this discourse, a demonstration of code implementation is presented. It is imperative to note that while the code may appear straightforward and seemingly rudimentary, its purpose lies in elucidating the underlying concept rather than serving as a testament to exceptional coding skills—an accomplishment I would undoubtedly fail to achieve.
A noteworthy tip on the horizon is the imminent ability to leverage multiple AI providers, thereby enriching the analysis process and augmenting its allure.
However, a word of caution is in order. It is crucial to meticulously test your code and formulate questions posed to the AI in a manner that ensures the reception of valid and trustworthy answers. Moreover, by implementing a semi-recursive loop (not demonstrated in the example code), AI itself can be employed to verify the authenticity and validity of the answers obtained.
In summary, our exploration of the integration of article analysis with ChatGPT underscores its immense potential. By continuously refining the underlying technologies, embracing the advancements on the horizon, and heeding the call for meticulous testing, we have the opportunity to revolutionize how users interact with news content. Through the collaborative efforts of humans and AI, we empower users with the ability to access, comprehend, and navigate the vast realm of news, all while ensuring the delivery of reliable, valuable, and enlightening insights.
The content is ordered as:
1.??????The Intro (as is done above).
2.??????The architecture.
3.??????The MVC Website page picture, showing the articles and analysis.
4.??????Use Cases.
5.??????Advice on road ahead and improvements
6.??????Summary.
7.??????The code.
The Architecture
领英推荐
The Azure MVC Web App to view articles
Could potentially be many articles (not shown)
Possible Use Cases for this functionality
Here is a list of possible use cases where an application that reads articles from a News API and utilizes ChatGPT to create an analysis per article can be useful:
These use cases highlight the versatility and potential applications of an application that combines a News API with ChatGPT's analysis capabilities. The specific implementation can be tailored to the needs of the target audience and domain.
Road ahead and advice for improvements of this functionality
To improve the functionality for the described use cases, here is some advice on the road ahead:
By following these guidelines, you can advance the functionality of the application, enhance the analysis capabilities, and provide users with valuable insights and a satisfying experience in accessing and understanding news articles.
Summary
In conclusion, the application that leverages a News API and ChatGPT's analysis capabilities offers a wide range of possibilities and benefits across various domains. By integrating these technologies, users can access article summaries and extensive analysis to make informed decisions, stay updated on topics of interest, and gain valuable insights from a vast amount of news content.
To further improve this functionality, it is crucial to focus on enhancing natural language processing capabilities, expanding data sources and APIs, and implementing entity recognition and topic extraction techniques. Fine-tuning the analysis for different domains, improving summarization techniques, and integrating multi-modal analysis will provide users with more accurate and comprehensive insights.
Real-time analysis, refined sentiment analysis, and opinion mining contribute to timely and nuanced understanding of news articles. By integrating user feedback and validation mechanisms, the application can continuously improve its accuracy and user trust. Visualization tools and ethical considerations should also be prioritized to present the analysis in an accessible and responsible manner.
Continuous evaluation, collaboration with AI researchers, and scalability optimization ensure the application's performance and adaptability in handling large-scale data processing. User-centric design and a seamless user experience further enhance the application's usability and effectiveness.
In summary, the combination of a News API and ChatGPT's analysis capabilities opens up numerous opportunities, such as news aggregation, personalized recommendations, trend analysis, sentiment tracking, and educational support. With ongoing improvements and a focus on user needs, this application can revolutionize the way users consume, understand, and engage with news content, empowering them to make well-informed decisions in an increasingly complex information landscape.
The Code
using NewsAPI
using NewsAPI.Models;
using NewsAPI.Constants;
using System;
using System.Net;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using static System.Net.Mime.MediaTypeNames;
using System.Globalization;
using OpenAI_API.Completions;
//-----------------------------------------------
// Author: Jaap Zwart
//
// Master in writing stupid code.
// Purpose of this stupid code:
// 1. Show how to read News articles from API
// 2. Show how to feed article info to ChatGPT
// 3. Show how to save info to Azure storage
// 4. Show how to read info from Azure storage
//
// Please ignore redundant code.
// Remember: it's dummy as dumb can be.
//-----------------------------------------------
namespace MyApplication
{
? ? class Program
? ? {
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? int addDays = -1; // We wanna get or save and clean storage data?
? ? ? ? ? ? DateTime currentDateTime = DateTime.Now;
? ? ? ? ? ? DateTime currentDate = currentDateTime.Date;
? ? ? ? ? ? string dateOnlyString = currentDate.AddDays(addDays).ToString().Replace(" 12:00:00 AM", "");
? ? ? ? ? ? // Get date and see if we need to update the content of the files.
? ? ? ? ? ? string getDate = ReadFirstLineFromFileInBlob("newscheckdate.txt");
? ? ? ? ? ? Console.WriteLine("From cloud:" + getDate);
? ? ? ? ? ? Console.WriteLine("From local:" + dateOnlyString);
? ? ? ? ? ? string SameD = "";
? ? ? ? ? ? if (dateOnlyString.Equals(getDate))
? ? ? ? ? ? ? ? SameD = "Same";
? ? ? ? ? ? else
? ? ? ? ? ? ? ? SameD = "Not Same";
? ? ? ? ? ? Console.WriteLine("DATE:" + getDate + "<>Found:" + SameD);
? ? ? ? ? ? string dd = DateTime.Now.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Second.ToString() +
? ? ? ? ? ? ? ? ? ? DateTime.Now.Minute.ToString() + "_new";
? ? ? ? ? ? // Meat of the method, do the article preparation.
? ? ? ? ? ? DoArticles("ChatGPT", SameD, currentDate, addDays);
? ? ? ? ? ? //DoArticles("AI", SameD, currentDate, addDays);
? ? ? ? ? ? //DoArticles("Google Bard", SameD, currentDate, addDays);
? ? ? ? ? ? // Reset date if not the same.
? ? ? ? ? ? if (SameD.Equals("Same"))
? ? ? ? ? ? {
? ? ? ? ? ? ? ?
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? // Update date in cloud
? ? ? ? ? ? ? ? ClearFileContentsInBlob("newscheckdate.txt");
? ? ? ? ? ? ? ? AppendTextToFileInBlob(dateOnlyString, "newscheckdate.txt");
? ? ? ? ? ? }
? ? ? ? ? ? Console.WriteLine("READY!");
? ? ? ? ? ? Console.ReadLine();
? ? ? ? }
? ? ? ? private static async void DoArticles(string whatS, string sameD, DateTime cDate, int aDays)
? ? ? ? {
? ? ? ? ? ? string SameD = sameD;
? ? ? ? ? ? DateTime currentDate = cDate;
? ? ? ? ? ? int addDays = aDays;
? ? ? ? ? ? // init with your API key
? ? ? ? ? ? var newsApiClient = new NewsApiClient("<YOUR KEY>");
? ? ? ? ? ? var articlesResponse = newsApiClient.GetEverything(new EverythingRequest
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Q = whatS,
? ? ? ? ? ? ? ? SortBy = SortBys.Popularity,
? ? ? ? ? ? ? ? Language = Languages.EN,
? ? ? ? ? ? ? ? From = currentDate.AddDays(addDays)
? ? ? ? ? ? });
? ? ? ? ? ?
? ? ? ? ? ? string getBlob = "";
? ? ? ? ? ? if (SameD.Equals("Same"))
? ? ? ? ? ? {
? ? ? ? ? ? ? ?
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? // When we have new day, clear Azure Storage files.
? ? ? ? ? ? ? ? ClearFileContentsInBlob("newsarticlesAI" + whatS.Replace(" ", "") + ".txt");
? ? ? ? ? ? ? ? ClearFileContentsInBlob("newsarticlesAI" + whatS.Replace(" ", "") + "Summary.txt");
? ? ? ? ? ? }
? ? ? ? ? ? if (articlesResponse.Status == Statuses.Ok)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (SameD.Equals("Same")) // Get existing articles
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? getBlob = ReadBlobContent("newsarticlesAI" + whatS.Replace(" ", "") + ".txt").Result;
? ? ? ? ? ? ? ? ? ? Console.WriteLine(getBlob);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else // Create new articles
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? // total results found
? ? ? ? ? ? ? ? ? ? Console.WriteLine(articlesResponse.TotalResults);
? ? ? ? ? ? ? ? ? ? // Forr when you wanna do some fancy stuff when doing analysis for all articles and get the last ones with mod.
? ? ? ? ? ? ? ? ? ? int number = articlesResponse.TotalResults;
? ? ? ? ? ? ? ? ? ? int remainder = number % 5;
? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? if (number >= 5)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? remainder = number % 5;
? ? ? ? ? ? ? ? ? ? ? ? int countCycle = number / 5;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? remainder = number;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? int i = 1;
? ? ? ? ? ? ? ? ? ? // Setting the stage. Vars, files and lift off....
? ? ? ? ? ? ? ? ? ? string getArticleForChatGPT = "";
? ? ? ? ? ? ? ? ? ? string getSummariesFromChatGPT = "";
? ? ? ? ? ? ? ? ? ? string getArticle = "<h1>" + whatS + "</h1>";
? ? ? ? ? ? ? ? ? ? AppendTextToFileInBlob(getArticle, "newsarticlesAI" + whatS.Replace(" ", "") + ".txt");
? ? ? ? ? ? ? ? ? ? AppendTextToFileInBlob(getArticle, "newsarticlesAI" + whatS.Replace(" ", "") + "Summary.txt");
? ? ? ? ? ? ? ? ? ? foreach (var article in articlesResponse.Articles)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? getArticle = "";
? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ? // title
? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine(whatS + ">>>>" + article.Title);
? ? ? ? ? ? ? ? ? ? ? ? getArticle = "<h3>" + article.Title + "</h3><br>";
? ? ? ? ? ? ? ? ? ? ? ? getArticleForChatGPT += article.Title + '\n';
? ? ? ? ? ? ? ? ? ? ? ? // author
? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine(whatS + ">>>>" + article.Author);
? ? ? ? ? ? ? ? ? ? ? ? getArticle += "<i>" + article.Author + "</i><br>";
? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ? // description
? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine(whatS + ">>>>" + article.Description);
? ? ? ? ? ? ? ? ? ? ? ? getArticle += article.Description + "<br>";
? ? ? ? ? ? ? ? ? ? ? ? getArticleForChatGPT += article.Title + '\n';
? ? ? ? ? ? ? ? ? ? ? ? // url
? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine(whatS + ">>>>" + article.Url);
? ? ? ? ? ? ? ? ? ? ? ? getArticle += "<a href='" + article.Url + "'>" + article.Title + "</a>" + "<br>";
? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ? // published at
? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine(whatS + ">>>>" + article.PublishedAt);
? ? ? ? ? ? ? ? ? ? ? ? getArticle += article.PublishedAt + "<br>";
? ? ? ? ? ? ? ? ? ? ? ? getArticle += "<hr>";
? ? ? ? ? ? ? ? ? ? ? ? AppendTextToFileInBlob(getArticle, "newsarticlesAI" + whatS.Replace(" ", "") + ".txt");
? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ? // Only do summaries for the first ...
? ? ? ? ? ? ? ? ? ? ? ? i += 1;
? ? ? ? ? ? ? ? ? ? ? ? if(i <= 10)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
? ? ? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine("MAKING SUMMARY");
? ? ? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
? ? ? ? ? ? ? ? ? ? ? ? ? ? string makeQuestionChatGPT = "Give an extensive analysis on: " + getArticleForChatGPT;
? ? ? ? ? ? ? ? ? ? ? ? ? ? getSummariesFromChatGPT = await GetChatGPT(makeQuestionChatGPT);
? ? ? ? ? ? ? ? ? ? ? ? ? ? AppendTextToFileInBlob(getArticle, "newsarticlesAI" + whatS.Replace(" ", "") + "Summary.txt");
? ? ? ? ? ? ? ? ? ? ? ? ? ? AppendTextToFileInBlob(getSummariesFromChatGPT + "<hr>", "newsarticlesAI" + whatS.Replace(" ", "") + "Summary.txt");
? ? ? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine(getSummariesFromChatGPT);
? ? ? ? ? ? ? ? ? ? ? ? ? ? getArticleForChatGPT = "";
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? Console.WriteLine("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
? ? ? ? ? ? Console.WriteLine("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
? ? ? ? ? ? Console.WriteLine("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
? ? ? ? }
? ? ? ? private async static Task<string> GetChatGPT(string textToAsk)
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? var openAI = new OpenAI_API.OpenAIAPI("<YOUR KEY>");
? ? ? ? ? ? ? ? CompletionRequest completion = new CompletionRequest();
? ? ? ? ? ? ? ? completion.Prompt = textToAsk;
? ? ? ? ? ? ? ? completion.MaxTokens = 4000;
? ? ? ? ? ? ? ? //completion.Model = "text-davinci-003"; // Set the model ID for GPT-3.5-turbo
? ? ? ? ? ? ? ? var result = await openAI.Completions.CreateCompletionAsync(completion);
? ? ? ? ? ? ? ? string answer = "";
? ? ? ? ? ? ? ? if (result != null)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? foreach (var item in result.Completions)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? answer += item.Text;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? return answer;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? return "No results from BlackBeltBible AI.";
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return ex.Message;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ?
? ? ? ? public static async Task<string> ReadBlobContent(string fileName)
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? string connS = "DefaultEndpointsProtocol=https;AccountName=<YOUR ACCOUNT NAME>;AccountKey=<YOUR ACCOUNT KEY>;EndpointSuffix=core.windows.net";
? ? ? ? ? ? ? ? CloudStorageAccount account = CloudStorageAccount.Parse(connS);
? ? ? ? ? ? ? ? var blobClient = account.CreateCloudBlobClient();
? ? ? ? ? ? ? ? var blobContainer = blobClient.GetContainerReference("newschannel");
? ? ? ? ? ? ? ? await blobContainer.CreateIfNotExistsAsync();
? ? ? ? ? ? ? ? CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(fileName);
? ? ? ? ? ? ? ? // Download the blob content asynchronously
? ? ? ? ? ? ? ? string blobContent = await blockBlob.DownloadTextAsync();
? ? ? ? ? ? ? ? return blobContent;
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return ex.Message;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ?
? ? ? ? public static string AppendTextToFileInBlob(string textToAppend, string fileName)
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? // Initialise client in a different place if you like
? ? ? ? ? ? ? ? string connS = "DefaultEndpointsProtocol=https;AccountName=<YOUR ACCOUNT NAME>;AccountKey=<YOUR ACCOUNT KEY>;EndpointSuffix=core.windows.net";
? ? ? ? ? ? ? ? CloudStorageAccount account = CloudStorageAccount.Parse(connS);
? ? ? ? ? ? ? ? var blobClient = account.CreateCloudBlobClient();
? ? ? ? ? ? ? ? // Make sure container is there
? ? ? ? ? ? ? ? var blobContainer = blobClient.GetContainerReference("newschannel");
? ? ? ? ? ? ? ? blobContainer.CreateIfNotExistsAsync();
? ? ? ? ? ? ? ? // Get the existing content of the file
? ? ? ? ? ? ? ? CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(fileName);
? ? ? ? ? ? ? ? string existingContent = blockBlob.DownloadTextAsync().GetAwaiter().GetResult();
? ? ? ? ? ? ? ? // Append the new text to the existing content
? ? ? ? ? ? ? ? string updatedContent = existingContent + textToAppend;
? ? ? ? ? ? ? ? // Upload the updated content back to the file
? ? ? ? ? ? ? ? blockBlob.UploadTextAsync(updatedContent).GetAwaiter().GetResult();
? ? ? ? ? ? ? ? return "Success";
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return ex.Message;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? public static string ClearFileContentsInBlob(string fileName)
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? string connS = "DefaultEndpointsProtocol=https;AccountName=<YOUR ACOUNT NAME>;AccountKey=<YOUR ACCOUNT KEY>;EndpointSuffix=core.windows.net";
? ? ? ? ? ? ? ? CloudStorageAccount account = CloudStorageAccount.Parse(connS);
? ? ? ? ? ? ? ? var blobClient = account.CreateCloudBlobClient();
? ? ? ? ? ? ? ? var blobContainer = blobClient.GetContainerReference("newschannel");
? ? ? ? ? ? ? ? blobContainer.CreateIfNotExistsAsync();
? ? ? ? ? ? ? ? CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(fileName);
? ? ? ? ? ? ? ? // Delete the existing blob
? ? ? ? ? ? ? ? blockBlob.DeleteIfExistsAsync();
? ? ? ? ? ? ? ? // Create a new empty blob with the same name
? ? ? ? ? ? ? ? blockBlob.UploadTextAsync("");
? ? ? ? ? ? ? ? return "Success";
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return ex.Message;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? public static string ReadFirstLineFromFileInBlob(string fileName)
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? // Initialise client in a different place if you like
? ? ? ? ? ? ? ? string connS = "DefaultEndpointsProtocol=https;AccountName=<YOUR ACCOUNT NAME>>;AccountKey=<YOUR ACCOUNT KEY>>;EndpointSuffix=core.windows.net";
? ? ? ? ? ? ? ? CloudStorageAccount account = CloudStorageAccount.Parse(connS);
? ? ? ? ? ? ? ? var blobClient = account.CreateCloudBlobClient();
? ? ? ? ? ? ? ? // Make sure container is there
? ? ? ? ? ? ? ? var blobContainer = blobClient.GetContainerReference("newschannel");
? ? ? ? ? ? ? ? blobContainer.CreateIfNotExistsAsync();
? ? ? ? ? ? ? ? // Get the content of the file
? ? ? ? ? ? ? ? CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(fileName);
? ? ? ? ? ? ? ? string fileContent = blockBlob.DownloadTextAsync().GetAwaiter().GetResult();
? ? ? ? ? ? ? ? // Extract the first line from the content
? ? ? ? ? ? ? ? string firstLine = fileContent.Split(Environment.NewLine)[0];
? ? ? ? ? ? ? ? return firstLine;
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return ex.Message;
? ? ? ? ? ? }
? ? ? ? }
? ? }
};
Code to read blob in Azure MVC Web app for the View. Controller Code.
?
public ActionResult ChatGPT(
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? HttpContext.Session["Action"] = "AnswersAINewsArticles";
? ? ? ? ? ? ? ? string SameD = "Same";
? ? ? ? ? ? ? ? HttpContext.Session["AllTheAnswers"] = "";
? ? ? ? ? ? ? ? if (SameD.Equals("Same")) // Get existing articles
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? var getBlob = readFileFromBlob("newsarticlesAIChatGPTSummary.txt");
? ? ? ? ? ? ? ? ? ? HttpContext.Session["AllTheAnswers"] += getBlob;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? HttpContext.Session["AllTheAnswers"] = ex.Message;
? ? ? ? ? ? }
? ? ? ? ? ? ViewBag.BookChapters = HttpContext.Session["AllTheAnswers"].ToString();
? ? ? ? ? ? return View();
? ? ? ? })
Azure MVC Web App View Code
@{
? ? ViewBag.Title = "Home Page";
}
<table>
? ? <tr>
? ? ? ? <td>
? ? ? ? ? ? <div style="text-align: justify;">
? ? ? ? ? ? ? ? <font size="3">
? ? ? ? ? ? ? ? ? ? @Html.Raw(ViewBag.BookChapters)
? ? ? ? ? ? ? ? </font>
? ? ? ? ? ? </div>
? ? ? ? </td>
? ? </tr>
</table>