Contenido principal

Extract Corporate Carbon Emission Target Metrics Using Ollama™

This example shows how to extract corporate carbon emission target metrics from company reports by using Ollama in streaming mode.

By default, when you pass a prompt to Ollama using ollamaChat, it generates a response internally and then outputs it in full at the end. To print out and format generated text as the model is generating it, use the StreamFun name-value argument of the ollamaChat class. The streaming function is a custom function handle that tells the model what to do with the output.

To run this example, you need a running Ollama server. As written, the example uses the llama3.2 model.

Print Stream Directly to Screen

In this example, the streamed output is printed directly to the screen.

Create the chat object with the stream function as a handle.

addpath 'D:\Chaeryon_example\matlab-deep-learning-llms-with-matlab-4.3.0.0'
chatPrintResponse = ollamaChat("llama3.2",StreamFun=@(token)fprintf("%s",token));

Sample paragraph from company report.

SampleParagraph = "Overall Net-Zero Target: HSS HIRE GROUP PLC commits to reach net-zero greenhouse gas emissions across the value chain by 2040. Near-Term Targets: HSS Hire Group PLC commits to reduce absolute scope 1 GHG emissions 46.5% by 2030 from a 2019 base year. HSS Hire Group PLC also commits to increase active annual sourcing of renewable electricity from 7.6% in 2019 to 100% by 2030. HSS Hire Group PLC further commits to reduce absolute scope 3 GHG emissions from use of sold products for sold fossil fuels 46.2% by 2030 from a 2019 base year. Finally, HSS Hire Group PLC commits to reduce all remaining absolute scope 3 GHG emissions 27.5% by 2030 from a 2019 base year. Long-Term Targets: HSS HIRE GROUP PLC commits to reduce absolute scope 1 GHG emissions 90% by 2040 from a 2019 base year. HSS HIRE GROUP PLC also commits to continue active annually sourcing 100% renewable electricity through 2040. HSS HIRE GROUP PLC further commits to reduce absolute scope 3 GHG emissions from use of sold products 90% by 2040 from a 2019 base year. Finally, HSS HIRE GROUP PLC commits to reduce all remaining absolute scope 3 GHG emissions 90% by 2040 from a 2019 base year."
SampleParagraph = 
"Overall Net-Zero Target: HSS HIRE GROUP PLC commits to reach net-zero greenhouse gas emissions across the value chain by 2040. Near-Term Targets: HSS Hire Group PLC commits to reduce absolute scope 1 GHG emissions 46.5% by 2030 from a 2019 base year. HSS Hire Group PLC also commits to increase active annual sourcing of renewable electricity from 7.6% in 2019 to 100% by 2030. HSS Hire Group PLC further commits to reduce absolute scope 3 GHG emissions from use of sold products for sold fossil fuels 46.2% by 2030 from a 2019 base year. Finally, HSS Hire Group PLC commits to reduce all remaining absolute scope 3 GHG emissions 27.5% by 2030 from a 2019 base year. Long-Term Targets: HSS HIRE GROUP PLC commits to reduce absolute scope 1 GHG emissions 90% by 2040 from a 2019 base year. HSS HIRE GROUP PLC also commits to continue active annually sourcing 100% renewable electricity through 2040. HSS HIRE GROUP PLC further commits to reduce absolute scope 3 GHG emissions from use of sold products 90% by 2040 from a 2019 base year. Finally, HSS HIRE GROUP PLC commits to reduce all remaining absolute scope 3 GHG emissions 90% by 2040 from a 2019 base year."

Generate response to extract near-term target year for reducing Green House Gas (GHG) emissions.

prompt = "Analyze the following paragraph to find the near-term target year for reducing the GHG emissions: " + SampleParagraph;
generate(chatPrintResponse, prompt, MaxNumTokens=500, Seed=100);
The near-term target year for reducing GHG emissions is 2030. There are three specific targets in the 2030 timeframe:

1. Reduce absolute scope 1 GHG emissions by 46.5%.
2. Increase active annual sourcing of renewable electricity from 7.6% to 100%.
3. Reduce absolute scope 3 GHG emissions from use of sold products for sold fossil fuels by 46.2%.

These targets are all in place by 2030, with the goal of achieving significant reductions in GHG emissions within the next decade.

Generate response to extract long-term target year for reducing Green House Gas (GHG) emissions.

prompt = "Analyze the following paragraph to find the long-term target year for reducing the GHG emissions: " + SampleParagraph;
generate(chatPrintResponse, prompt, MaxNumTokens=500, Seed=100);
The long-term target year for reducing GHG emissions is 2040.

Generate response in JSON format to extract both near-term target year and long-term target year for reducing Green House Gas (GHG) emissions..

prompt = "Analyze the following paragraph to find the near-term target year and long-term target year for reducing the GHG emissions: " + SampleParagraph ..." + ...
    + "Format your output in JSON with keys near_term_target_year and long_term_target_year"
prompt = 
"Analyze the following paragraph to find the near-term target year and long-term target year for reducing the GHG emissions: Overall Net-Zero Target: HSS HIRE GROUP PLC commits to reach net-zero greenhouse gas emissions across the value chain by 2040. Near-Term Targets: HSS Hire Group PLC commits to reduce absolute scope 1 GHG emissions 46.5% by 2030 from a 2019 base year. HSS Hire Group PLC also commits to increase active annual sourcing of renewable electricity from 7.6% in 2019 to 100% by 2030. HSS Hire Group PLC further commits to reduce absolute scope 3 GHG emissions from use of sold products for sold fossil fuels 46.2% by 2030 from a 2019 base year. Finally, HSS Hire Group PLC commits to reduce all remaining absolute scope 3 GHG emissions 27.5% by 2030 from a 2019 base year. Long-Term Targets: HSS HIRE GROUP PLC commits to reduce absolute scope 1 GHG emissions 90% by 2040 from a 2019 base year. HSS HIRE GROUP PLC also commits to continue active annually sourcing 100% renewable electricity through 2040. HSS HIRE GROUP PLC further commits to reduce absolute scope 3 GHG emissions from use of sold products 90% by 2040 from a 2019 base year. Finally, HSS HIRE GROUP PLC commits to reduce all remaining absolute scope 3 GHG emissions 90% by 2040 from a 2019 base year.Format your output in JSON with keys near_term_target_year and long_term_target_year"
generate(chatPrintResponse, prompt, MaxNumTokens=500, Seed=100, ResponseFormat="json");
{"near_term_target_year": "2030", "long_term_target_year": "2040"}

Store Extracted Metric to a File

In this example, the streamed output is stored in a JSON file for further processing.

if exist("metric.json")~=0
    delete("metric.json")
end

function writeToken(token)
fileID = fopen('metric.json','a');
fprintf(fileID,"%s",token);
fclose(fileID);
end

Create the chat object with the stream function as a handle.

chatWriteFile = ollamaChat("llama3.2",StreamFun=@writeToken);
generate(chatWriteFile, prompt, MaxNumTokens=500, Seed=100, ResponseFormat="json");

fileID = fopen('metric.json','r');
StoredMetric = textscan(fileID,"%s");
[StoredMetric{1}{:}]
ans = 
'{"near_term_target_year":2030,"long_term_target_year":2040}'
fclose(fileID);