diff --git a/010_course_material/01_course_introduction.md b/010_course_material/01_course_introduction.md new file mode 100644 index 0000000..54a0e07 --- /dev/null +++ b/010_course_material/01_course_introduction.md @@ -0,0 +1,13 @@ +# Generative AI with Python + +## 1. Course Introduction + +### 1.1 Course Overview + +![1770304750827.png](./images/1770304750827.png) + +### System Setup + +![1770317842758.png](./images/1770317842758.png) + +![1770318020099.png](./images/1770318020099.png) diff --git a/010_course_material/02_llm_introduction.md b/010_course_material/02_llm_introduction.md new file mode 100644 index 0000000..f19bd0d --- /dev/null +++ b/010_course_material/02_llm_introduction.md @@ -0,0 +1,27 @@ +# Generative AI with Python + +## 2. LLM - Introduction + +### 2.1 LLM Introduction + +![1770389187224.png](./images/1770389187224.png) + +![1770389217431.png](./images/1770389217431.png) + +![1770389507588.png](./images/1770389507588.png) + +![1770389567212.png](./images/1770389567212.png) + +### 2.2 Classical NLP Models vs. LLM + +![1770389939080.png](./images/1770389939080.png) + +![1770389959448.png](./images/1770389959448.png) + +### 2.3 Narrow AI Achievements + +![1770390278773.png](./images/1770390278773.png) + +### 2.4 Model Performance and Capabilities + +![1770391230138.png](./images/1770391230138.png) diff --git a/010_course_material/03_llm_deep_dive.md b/010_course_material/03_llm_deep_dive.md new file mode 100644 index 0000000..87af9ab --- /dev/null +++ b/010_course_material/03_llm_deep_dive.md @@ -0,0 +1,102 @@ +# Generative AI with Python + +## 3. LLM - Deep Dive + +### 3.1 Model Training Process + +![1776035231939.png](./1776035231939.png) + +![1776035369175.png](./1776035369175.png) + +Instead of just extending sentences by adding new words, LLMs are now trained on large corpuses of instruction dataset that teaches the model how to respond to different instructions. + +![1776035383359.png](./1776035383359.png) + +![1776035515821.png](./1776035515821.png) + +### 3.2 Model Improvement Options + +Now, we're going to take a look at different ways to improve the model output.And here this is already a foresighton different sections that we're going to cover at some later point.But first things first,if you use a model and you are not satisfied with the outputbecause you did not put enough effort into your user query in the first place.So that would be the simplest part called direct prompting.And we will see in this lecturehow you could improve the output.So the first thing would bethat you want to improve it bymodifying at the beginning of this chain.So the train is that you have the user query, it's passed to the LMand the LLM is providing some output.So one thing would bethat the problem is at the beginning of the train,if the beginning of the train is that you were quite sloppy in providing the queryand it was maybe not specific enough.There are many different waysto improve your query.All of this is running under the umbrella called Prompt Engineering.So you can put a lot of effort into tuning your prompt.Andthat would be all trying to change the chain at the beginning.But this might not change or might not solve the problemif the model doesn't have the answer. So, if in the model weights the answer is not included,then you cannot solve it with prompt engineering.And herethe technique called retrieval augmented generation is coming into play.This is working in the following wayand we will spend a lot of time on this.But here just quickly on a very high level,we are starting out again with the user query.Andthen the user query is passedin two different ways to the model.One way is the direct passing to the large language model.The other one is that you make a bypassand the user query is passed to some external sourcefrom the external source which could be a vector database or an internet search,you get back certain results which are very relevant for answering the question.Thenthe model is provided with this output as well.And with this contextual information,the model is now capable of answering the questionand providing a good output.So this isthe core idea of retriever augmented generation.And in the majority of cases, this is the way to go.But in specific cases, it might be thatfine tuning. The modelis a better approach.In that case, it would be two stagesthe first stage would be this horizontal path.So you would start out with your external data source.It is passed to the large language modeland some so called finetuned large language model is created.So you can see here now that thisinformation from the external sourceis now going to end up as**parametric weights of the model.**And now once the model has been trained,the query is running as usual.So the user is passing inthe question in its query,then this is passed to the finetuned large language modeland the model itself is then providing the output.So this would be the idea of finetuning some large language modeland the difficulty is from left to right. So the most simple one is of course direct prompting,putting not much effort into it,but you are not satisfied with the result. Thenprompt engineering is the nextmore difficult approachRsystems are still a bit more difficult. But again,with the difficulty, the performance is increasingand fine tuning is probably the most complicatedpart of it.And as I said, usually not necessary. So, we are going to work mostly with Rsystems in the scope. + +![1776035778454.png](./1776035778454.png) + +### 3.3 Model Providers + +![1776036637966.png](./1776036637966.png) + +### 3.4 Model Benchmarking + +![1776036693109.png](./1776036693109.png) + +LLM Leaderboard: [https://arena.ai/leaderboard](https://https://arena.ai/leaderboard) + +![1776036879224.png](./1776036879224.png) + +![1776036893209.png](./1776036893209.png) + +### 3.5 Interaction with LLMs + +#### 3.5.1 Python + +![1776254636958.png](./1776254636958.png) + +![1776254663109.png](./1776254663109.png) + +#### 3.5.2 Groq + +![1776255870273.png](./1776255870273.png) + +![1776260183492.png](./1776260183492.png) + +#### 3.5.3 OpenAI + +[LangChain](https://docs.langchain.com/oss/python/langchain/overviewhttps://) + +![1776260430717.png](./1776260430717.png) + +![1776260488538.png](./1776260488538.png) + +![1776260542065.png](./1776260542065.png) + +![1776260557979.png](./1776260557979.png) + +![1776260757283.png](./1776260757283.png)**** + +![1776260814914.png](./1776260814914.png) + +[OpenAI Models](https://developers.openai.com/api/docs/modelshttps://) + +![1776261601248.png](./1776261601248.png) + +#### 3.5.4 Gemini + + + +![1776264998797.png](./1776264998797.png) + +### 3.6 Message Types + + ![1776265312028.png](./1776265312028.png) + + ![1776267895497.png](./1776267895497.png) + + ![1776268495732.png](./1776268495732.png) + +[Adding a System Message to Groq](https://console.groq.com/playgroundhttps://) + + ![1776268821290.png](./1776268821290.png) + +### 3.7 LLM Parameters + + ![1776269156247.png](./1776269156247.png) + + ![1776270468357.png](./1776270468357.png) + + ![1776270608661.png](./1776270608661.png) + + ![1776270848195.png](./1776270848195.png) + +Using Groq playground to change the model parameters. + + + ![1776271024282.png](./1776271024282.png) diff --git a/010_course_material/04_llm_types_and_variants.md b/010_course_material/04_llm_types_and_variants.md new file mode 100644 index 0000000..8dc5129 --- /dev/null +++ b/010_course_material/04_llm_types_and_variants.md @@ -0,0 +1,63 @@ +# Generative AI with Python + +## 4. LLM - Types and Variants + +### 4.1 Local Use of Models + +![1776278354938.png](./1776278354938.png) + +![1776278368194.png](./1776278368194.png) + +### 4.2 Large Multimodal Models + +![1776279206115.png](./1776279206115.png) + +![1776279399577.png](./1776279399577.png) + +### 4.3 Large Video Models + + ![1776678626780.png](./1776678626780.png) + + ![1776678670330.png](./1776678670330.png) + + ![1776678702041.png](./1776678702041.png) + +### 4.4 Tokenization + + ![1776678737727.png](./1776678737727.png) + + ![1776678775902.png](./1776678775902.png) + + ![1776678827341.png](./1776678827341.png) + +Sub-word Tokenization + + ![1776678889441.png](./1776678889441.png) + +[Open AI Tokenizer](https://platform.openai.com/tokenizerhttps://) + + ![1776678982087.png](./1776678982087.png) + +### 4.5 Reasoning Models + +In the case of reasoning models, we can see that the model is generating intermediate steps using chain of thoughts and then it uses several tokens to develop the logic, before generating the output. There is a possibility that the number of tokens it uses in generating the logic ends up consuming the tokens, hence the context window, so the output needs to be truncated. + + ![1776679249642.png](./1776679249642.png) + + ![1776679330183.png](./1776679330183.png) + +### 4.6 Small Language Models + +SLMs may be able to compete with LLMs because they are trained on very well curated datasets. They spend less time in training, but they tend to spend more time in inferences. + + ![1776679622848.png](./1776679622848.png) + +### 4.7 Jailbreaking + +This uses a technique called **Art Prompt** + + ![1776679784309.png](./1776679784309.png) + +This uses a technique called **Math Prompt** + + ![1776679880161.png](./1776679880161.png) diff --git a/010_course_material/05_llm_chains.md b/010_course_material/05_llm_chains.md new file mode 100644 index 0000000..e69de29 diff --git a/010_course_material/06_vector_databases.md b/010_course_material/06_vector_databases.md new file mode 100644 index 0000000..e69de29 diff --git a/010_course_material/07_rag_baseline.md b/010_course_material/07_rag_baseline.md new file mode 100644 index 0000000..e69de29 diff --git a/010_course_material/08_rag_advanced.md b/010_course_material/08_rag_advanced.md new file mode 100644 index 0000000..e69de29 diff --git a/010_course_material/09_agentic_systems_overview.md b/010_course_material/09_agentic_systems_overview.md new file mode 100644 index 0000000..e69de29 diff --git a/010_course_material/10_agentic_systems_crewAI.md b/010_course_material/10_agentic_systems_crewAI.md new file mode 100644 index 0000000..e69de29 diff --git a/010_course_material/11_agentic_systems_ag2.md b/010_course_material/11_agentic_systems_ag2.md new file mode 100644 index 0000000..e69de29 diff --git a/010_course_material/12_agentic_systems_openAI.md b/010_course_material/12_agentic_systems_openAI.md new file mode 100644 index 0000000..e69de29 diff --git a/010_course_material/13_agentic_systems_googleSDK.md b/010_course_material/13_agentic_systems_googleSDK.md new file mode 100644 index 0000000..e69de29 diff --git a/010_course_material/14_agent_interactions.md b/010_course_material/14_agent_interactions.md new file mode 100644 index 0000000..e69de29 diff --git a/010_course_material/15_model_finetuning.md b/010_course_material/15_model_finetuning.md new file mode 100644 index 0000000..e69de29 diff --git a/010_course_material/1776035231939.png b/010_course_material/1776035231939.png new file mode 100644 index 0000000..e88be9d Binary files /dev/null and b/010_course_material/1776035231939.png differ diff --git a/010_course_material/1776035369175.png b/010_course_material/1776035369175.png new file mode 100644 index 0000000..c9f32e1 Binary files /dev/null and b/010_course_material/1776035369175.png differ diff --git a/010_course_material/1776035383359.png b/010_course_material/1776035383359.png new file mode 100644 index 0000000..b307c65 Binary files /dev/null and b/010_course_material/1776035383359.png differ diff --git a/010_course_material/1776035515821.png b/010_course_material/1776035515821.png new file mode 100644 index 0000000..2358b2f Binary files /dev/null and b/010_course_material/1776035515821.png differ diff --git a/010_course_material/1776035778454.png b/010_course_material/1776035778454.png new file mode 100644 index 0000000..326ed60 Binary files /dev/null and b/010_course_material/1776035778454.png differ diff --git a/010_course_material/1776036637966.png b/010_course_material/1776036637966.png new file mode 100644 index 0000000..6d3e581 Binary files /dev/null and b/010_course_material/1776036637966.png differ diff --git a/010_course_material/1776036693109.png b/010_course_material/1776036693109.png new file mode 100644 index 0000000..733e75f Binary files /dev/null and b/010_course_material/1776036693109.png differ diff --git a/010_course_material/1776036875089.png b/010_course_material/1776036875089.png new file mode 100644 index 0000000..1b6a122 Binary files /dev/null and b/010_course_material/1776036875089.png differ diff --git a/010_course_material/1776036879224.png b/010_course_material/1776036879224.png new file mode 100644 index 0000000..1b6a122 Binary files /dev/null and b/010_course_material/1776036879224.png differ diff --git a/010_course_material/1776036893209.png b/010_course_material/1776036893209.png new file mode 100644 index 0000000..bed9803 Binary files /dev/null and b/010_course_material/1776036893209.png differ diff --git a/010_course_material/1776254636958.png b/010_course_material/1776254636958.png new file mode 100644 index 0000000..958c020 Binary files /dev/null and b/010_course_material/1776254636958.png differ diff --git a/010_course_material/1776254663109.png b/010_course_material/1776254663109.png new file mode 100644 index 0000000..cbb2485 Binary files /dev/null and b/010_course_material/1776254663109.png differ diff --git a/010_course_material/1776255870273.png b/010_course_material/1776255870273.png new file mode 100644 index 0000000..85cf62c Binary files /dev/null and b/010_course_material/1776255870273.png differ diff --git a/010_course_material/1776260180511.png b/010_course_material/1776260180511.png new file mode 100644 index 0000000..59cf1e9 Binary files /dev/null and b/010_course_material/1776260180511.png differ diff --git a/010_course_material/1776260183492.png b/010_course_material/1776260183492.png new file mode 100644 index 0000000..59cf1e9 Binary files /dev/null and b/010_course_material/1776260183492.png differ diff --git a/010_course_material/1776260430717.png b/010_course_material/1776260430717.png new file mode 100644 index 0000000..2d93eee Binary files /dev/null and b/010_course_material/1776260430717.png differ diff --git a/010_course_material/1776260488538.png b/010_course_material/1776260488538.png new file mode 100644 index 0000000..49f0b04 Binary files /dev/null and b/010_course_material/1776260488538.png differ diff --git a/010_course_material/1776260542065.png b/010_course_material/1776260542065.png new file mode 100644 index 0000000..53c57d3 Binary files /dev/null and b/010_course_material/1776260542065.png differ diff --git a/010_course_material/1776260557979.png b/010_course_material/1776260557979.png new file mode 100644 index 0000000..063ae1b Binary files /dev/null and b/010_course_material/1776260557979.png differ diff --git a/010_course_material/1776260757283.png b/010_course_material/1776260757283.png new file mode 100644 index 0000000..799149f Binary files /dev/null and b/010_course_material/1776260757283.png differ diff --git a/010_course_material/1776260814914.png b/010_course_material/1776260814914.png new file mode 100644 index 0000000..6ce2c5f Binary files /dev/null and b/010_course_material/1776260814914.png differ diff --git a/010_course_material/1776261601248.png b/010_course_material/1776261601248.png new file mode 100644 index 0000000..f5ef430 Binary files /dev/null and b/010_course_material/1776261601248.png differ diff --git a/010_course_material/1776264998797.png b/010_course_material/1776264998797.png new file mode 100644 index 0000000..3c8ae93 Binary files /dev/null and b/010_course_material/1776264998797.png differ diff --git a/010_course_material/1776265312028.png b/010_course_material/1776265312028.png new file mode 100644 index 0000000..00a7390 Binary files /dev/null and b/010_course_material/1776265312028.png differ diff --git a/010_course_material/1776267895497.png b/010_course_material/1776267895497.png new file mode 100644 index 0000000..132a2c3 Binary files /dev/null and b/010_course_material/1776267895497.png differ diff --git a/010_course_material/1776268495732.png b/010_course_material/1776268495732.png new file mode 100644 index 0000000..f703e9c Binary files /dev/null and b/010_course_material/1776268495732.png differ diff --git a/010_course_material/1776268821290.png b/010_course_material/1776268821290.png new file mode 100644 index 0000000..18b1fbd Binary files /dev/null and b/010_course_material/1776268821290.png differ diff --git a/010_course_material/1776269156247.png b/010_course_material/1776269156247.png new file mode 100644 index 0000000..5d6f093 Binary files /dev/null and b/010_course_material/1776269156247.png differ diff --git a/010_course_material/1776270468357.png b/010_course_material/1776270468357.png new file mode 100644 index 0000000..856d17b Binary files /dev/null and b/010_course_material/1776270468357.png differ diff --git a/010_course_material/1776270608661.png b/010_course_material/1776270608661.png new file mode 100644 index 0000000..6f14ad0 Binary files /dev/null and b/010_course_material/1776270608661.png differ diff --git a/010_course_material/1776270848195.png b/010_course_material/1776270848195.png new file mode 100644 index 0000000..12d4aa8 Binary files /dev/null and b/010_course_material/1776270848195.png differ diff --git a/010_course_material/1776271024282.png b/010_course_material/1776271024282.png new file mode 100644 index 0000000..e19e534 Binary files /dev/null and b/010_course_material/1776271024282.png differ diff --git a/010_course_material/1776271082713.png b/010_course_material/1776271082713.png new file mode 100644 index 0000000..6cf2257 Binary files /dev/null and b/010_course_material/1776271082713.png differ diff --git a/010_course_material/1776271087697.png b/010_course_material/1776271087697.png new file mode 100644 index 0000000..6cf2257 Binary files /dev/null and b/010_course_material/1776271087697.png differ diff --git a/010_course_material/1776271145448.png b/010_course_material/1776271145448.png new file mode 100644 index 0000000..07a9c21 Binary files /dev/null and b/010_course_material/1776271145448.png differ diff --git a/010_course_material/1776273124425.png b/010_course_material/1776273124425.png new file mode 100644 index 0000000..06e069f Binary files /dev/null and b/010_course_material/1776273124425.png differ diff --git a/010_course_material/1776273213888.png b/010_course_material/1776273213888.png new file mode 100644 index 0000000..209fc79 Binary files /dev/null and b/010_course_material/1776273213888.png differ diff --git a/010_course_material/1776273327240.png b/010_course_material/1776273327240.png new file mode 100644 index 0000000..5875a0f Binary files /dev/null and b/010_course_material/1776273327240.png differ diff --git a/010_course_material/1776273498200.png b/010_course_material/1776273498200.png new file mode 100644 index 0000000..ba5419f Binary files /dev/null and b/010_course_material/1776273498200.png differ diff --git a/010_course_material/1776273524991.png b/010_course_material/1776273524991.png new file mode 100644 index 0000000..b8370ca Binary files /dev/null and b/010_course_material/1776273524991.png differ diff --git a/010_course_material/1776278354938.png b/010_course_material/1776278354938.png new file mode 100644 index 0000000..6068afa Binary files /dev/null and b/010_course_material/1776278354938.png differ diff --git a/010_course_material/1776278368194.png b/010_course_material/1776278368194.png new file mode 100644 index 0000000..edf6365 Binary files /dev/null and b/010_course_material/1776278368194.png differ diff --git a/010_course_material/1776279206115.png b/010_course_material/1776279206115.png new file mode 100644 index 0000000..63d610c Binary files /dev/null and b/010_course_material/1776279206115.png differ diff --git a/010_course_material/1776279399577.png b/010_course_material/1776279399577.png new file mode 100644 index 0000000..999bd6c Binary files /dev/null and b/010_course_material/1776279399577.png differ diff --git a/010_course_material/1776678626780.png b/010_course_material/1776678626780.png new file mode 100644 index 0000000..4394828 Binary files /dev/null and b/010_course_material/1776678626780.png differ diff --git a/010_course_material/1776678670330.png b/010_course_material/1776678670330.png new file mode 100644 index 0000000..ce4a089 Binary files /dev/null and b/010_course_material/1776678670330.png differ diff --git a/010_course_material/1776678702041.png b/010_course_material/1776678702041.png new file mode 100644 index 0000000..016347a Binary files /dev/null and b/010_course_material/1776678702041.png differ diff --git a/010_course_material/1776678737727.png b/010_course_material/1776678737727.png new file mode 100644 index 0000000..989e471 Binary files /dev/null and b/010_course_material/1776678737727.png differ diff --git a/010_course_material/1776678775902.png b/010_course_material/1776678775902.png new file mode 100644 index 0000000..2d39319 Binary files /dev/null and b/010_course_material/1776678775902.png differ diff --git a/010_course_material/1776678827341.png b/010_course_material/1776678827341.png new file mode 100644 index 0000000..e941728 Binary files /dev/null and b/010_course_material/1776678827341.png differ diff --git a/010_course_material/1776678889441.png b/010_course_material/1776678889441.png new file mode 100644 index 0000000..801ff38 Binary files /dev/null and b/010_course_material/1776678889441.png differ diff --git a/010_course_material/1776678982087.png b/010_course_material/1776678982087.png new file mode 100644 index 0000000..4b69041 Binary files /dev/null and b/010_course_material/1776678982087.png differ diff --git a/010_course_material/1776679249642.png b/010_course_material/1776679249642.png new file mode 100644 index 0000000..a1418a5 Binary files /dev/null and b/010_course_material/1776679249642.png differ diff --git a/010_course_material/1776679330183.png b/010_course_material/1776679330183.png new file mode 100644 index 0000000..9c0c055 Binary files /dev/null and b/010_course_material/1776679330183.png differ diff --git a/010_course_material/1776679622848.png b/010_course_material/1776679622848.png new file mode 100644 index 0000000..90b22e1 Binary files /dev/null and b/010_course_material/1776679622848.png differ diff --git a/010_course_material/1776679784309.png b/010_course_material/1776679784309.png new file mode 100644 index 0000000..b4bec84 Binary files /dev/null and b/010_course_material/1776679784309.png differ diff --git a/010_course_material/1776679880161.png b/010_course_material/1776679880161.png new file mode 100644 index 0000000..f968f7c Binary files /dev/null and b/010_course_material/1776679880161.png differ diff --git a/010_course_material/images/1770304750827.png b/010_course_material/images/1770304750827.png new file mode 100644 index 0000000..25ad34c Binary files /dev/null and b/010_course_material/images/1770304750827.png differ diff --git a/010_course_material/images/1770317842758.png b/010_course_material/images/1770317842758.png new file mode 100644 index 0000000..0dae151 Binary files /dev/null and b/010_course_material/images/1770317842758.png differ diff --git a/010_course_material/images/1770318020099.png b/010_course_material/images/1770318020099.png new file mode 100644 index 0000000..8fca73e Binary files /dev/null and b/010_course_material/images/1770318020099.png differ diff --git a/010_course_material/images/1770389187224.png b/010_course_material/images/1770389187224.png new file mode 100644 index 0000000..4c150b7 Binary files /dev/null and b/010_course_material/images/1770389187224.png differ diff --git a/010_course_material/images/1770389217431.png b/010_course_material/images/1770389217431.png new file mode 100644 index 0000000..01f56ac Binary files /dev/null and b/010_course_material/images/1770389217431.png differ diff --git a/010_course_material/images/1770389507588.png b/010_course_material/images/1770389507588.png new file mode 100644 index 0000000..d3e9f1a Binary files /dev/null and b/010_course_material/images/1770389507588.png differ diff --git a/010_course_material/images/1770389567212.png b/010_course_material/images/1770389567212.png new file mode 100644 index 0000000..7a94799 Binary files /dev/null and b/010_course_material/images/1770389567212.png differ diff --git a/010_course_material/images/1770389939080.png b/010_course_material/images/1770389939080.png new file mode 100644 index 0000000..4a2c9a9 Binary files /dev/null and b/010_course_material/images/1770389939080.png differ diff --git a/010_course_material/images/1770389959448.png b/010_course_material/images/1770389959448.png new file mode 100644 index 0000000..12f3e7c Binary files /dev/null and b/010_course_material/images/1770389959448.png differ diff --git a/010_course_material/images/1770390278773.png b/010_course_material/images/1770390278773.png new file mode 100644 index 0000000..e7b18fd Binary files /dev/null and b/010_course_material/images/1770390278773.png differ diff --git a/010_course_material/images/1770391230138.png b/010_course_material/images/1770391230138.png new file mode 100644 index 0000000..ea1995e Binary files /dev/null and b/010_course_material/images/1770391230138.png differ diff --git a/010_course_material/images/G.jpg b/010_course_material/images/G.jpg new file mode 100644 index 0000000..5c58aea Binary files /dev/null and b/010_course_material/images/G.jpg differ diff --git a/020_llm/28a_start_model_chat_groq.py b/020_llm/28a_start_model_chat_groq.py index d6050ab..0c5c7e3 100644 --- a/020_llm/28a_start_model_chat_groq.py +++ b/020_llm/28a_start_model_chat_groq.py @@ -1,13 +1,33 @@ #%% packages +import os +from groq import Groq +from langchain_groq import ChatGroq +from dotenv import load_dotenv, find_dotenv +load_dotenv(find_dotenv(usecwd = True)) +# %% +# %% +api_key = api_key=os.getenv("GROQ_API_KEY") +# %% +# Fetches models available on Groq's LPU inference engine +client = Groq(api_key=api_key) +response = client.models.list() + +print("--- Groq Models ---") +for model in response.data: + # Groq provides additional metadata like 'context_window' + print(f"ID: {model.id:30} | Developer: {model.owned_by}") # %% # Model overview: https://console.groq.com/docs/models - +MODEL_NAME = "llama-3.3-70b-versatile" +model = ChatGroq(model=MODEL_NAME, + temperature=0.7, + api_key=api_key) # %% Run the model - +res = model.invoke("What is the capital of France?") # %% find out what is in the result - +res.model_dump() # %% only print content - +res.content # %% diff --git a/020_llm/28b_start_model_chat_openai.py b/020_llm/28b_start_model_chat_openai.py index cdcedee..44065bb 100644 --- a/020_llm/28b_start_model_chat_openai.py +++ b/020_llm/28b_start_model_chat_openai.py @@ -1,14 +1,21 @@ #%% packages import os from dotenv import load_dotenv, find_dotenv +from langchain_openai import ChatOpenAI load_dotenv(find_dotenv(usecwd=True)) +from openai import OpenAI #TODO: add package import +# %% +# Retrieve API key from environment variable +api_key=os.getenv("OPENAI_API_KEY") + # %% # Model overview: https://console.groq.com/docs/models MODEL_NAME = 'gpt-4o-mini' #TODO: add the model - - +model = ChatOpenAI(model=MODEL_NAME, + temperature=0.7, + api_key=api_key) # %% Run the model res = model.invoke("What is a Generative AI?") # %% find out what is in the result @@ -16,3 +23,10 @@ # %% only print content print(res.content) # %% +client = OpenAI(api_key=api_key) +models = client.models.list() + +print("--- OpenAI Models ---") +for model in models: + print(f"Model ID: {model.id} | Owned By: {model.owned_by}") +# %% diff --git a/020_llm/28c_start_model_chat_gemini.py b/020_llm/28c_start_model_chat_gemini.py index cfa4c21..6667d7c 100644 --- a/020_llm/28c_start_model_chat_gemini.py +++ b/020_llm/28c_start_model_chat_gemini.py @@ -3,12 +3,25 @@ from dotenv import load_dotenv, find_dotenv load_dotenv(find_dotenv(usecwd=True)) # TODO: add package import +import google.generativeai as genai +from langchain_google_genai import ChatGoogleGenerativeAI +# %% +api_key = api_key=os.getenv("GOOGLE_API_KEY") +# %% +# List all models +genai.configure(api_key=api_key) +for m in genai.list_models(): + if 'generateContent' in m.supported_generation_methods: + print(m.name) # %% # Model overview: https://ai.google.dev/models/gemini -MODEL_NAME = 'gemini-1.5-flash' +MODEL_NAME = 'gemini-2.5-flash' # TODO: add the model +model = ChatGoogleGenerativeAI(model=MODEL_NAME, + temperature=0.7, + api_key=api_key) # %% Run the model res = model.invoke("What is a Generative AI?") # %% find out what is in the result diff --git a/020_llm/45_start_local_use.py b/020_llm/45_start_local_use.py index bc1d999..ec8d71b 100644 --- a/020_llm/45_start_local_use.py +++ b/020_llm/45_start_local_use.py @@ -4,9 +4,13 @@ # !ollama pull qwen3:4b # %% model setup -model = ChatOllama(model="qwen3:4b", temperature=0.2, extract_reasoning=True) +model = ChatOllama(model="deepseek-r1:latest", + temperature=0.2, + extract_reasoning=True) # %% invoke model res = model.invoke("What is Ollama?") # %% res.model_dump() +# %% only print content +print(res.content) # %% diff --git a/020_llm/47_start_large_multimodal_models.py b/020_llm/47_start_large_multimodal_models.py index 266fdee..ee83754 100644 --- a/020_llm/47_start_large_multimodal_models.py +++ b/020_llm/47_start_large_multimodal_models.py @@ -1,10 +1,53 @@ # source: https://console.groq.com/docs/vision #%% packages - +from groq import Groq +import base64 +import os #%% Function to encode the image / model invocation +def encode_image(image_path): + """ + This function reads an image file and encodes it in base64 format, + which is suitable for sending to the Groq API. The encoded string + can then be included in the request payload when invoking the model. + """ + with open(image_path, "rb") as image_file: + return base64.b64encode(image_file.read()).decode("utf-8") + +# image_path = "rag_workflow.png" +image_path = "../images/G.jpg" # Path to your image +user_question = "How many documents are shown in this image?" +base64_image = encode_image(image_path) +# %% +api_key = os.getenv("GROQ_API_KEY") +if not api_key: + raise ValueError("GROQ_API_KEY not found in environment variables.") +client = Groq(api_key=api_key) +try: + chat_completion = client.chat.completions.create( + messages=[ + { + "role": "user", + "content": [ + {"type": "text", "text": user_question}, + { + "type": "image_url", + "image_url": { + "url": f"data:image/png;base64,{base64_image}" + }, + }, + ], + }, + ], + model="meta-llama/llama-4-scout-17b-16e-instruct", + ) -#%% check the response +except Exception as e: + print(f"An error occurred: {e}") +# %% +print("Model Output:\n") +print(chat_completion.choices[0].message.content) +# %%