forked from Helsinki-NLP/LLM-course-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Conflicts: # README.md
- Loading branch information
Showing
22 changed files
with
14,298 additions
and
7,089 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -240,3 +240,6 @@ cython_debug/ | |
.idea/ | ||
|
||
*.pyc | ||
.sesskey | ||
|
||
*.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,16 @@ | ||
# Introduction to Generative AI and Large Language Models (LLM) | ||
# Introduction to Generative AI and Large Language Models (LLM) | ||
|
||
Week 1 is the most theory-heavy week of the course. You can find the lecture slides here: [Week 1 Slides](https://github.com/Helsinki-NLP/LLM-course-2024/blob/main/week-1/LLM-Course%20Lecture%201.pdf). | ||
|
||
## Lab Assignment | ||
|
||
Research on Tokenizers and write a section to your final report reflecting on the following questions: | ||
* What are tokenizers? | ||
* Why are they important for language modeling and LLMs? | ||
* What different tokenization algorithms are there and which ones are the most popular ones and why? | ||
|
||
**Some references:** | ||
* Neural Machine Translation of Rare Words with Subword Units: | ||
[https://arxiv.org/abs/1508.07909](https://arxiv.org/abs/1508.07909) | ||
* SentencePiece: A simple and language independent subword tokenizer and detokenizer for Neural Text Processing: | ||
[https://arxiv.org/abs/1808.06226](https://arxiv.org/abs/1808.06226) |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,34 @@ | ||
# Using LLMs and Prompting-based approaches | ||
# Using LLMs and Prompting-based approaches | ||
|
||
## Lecture | ||
|
||
Slides can be found here: [Week 2 Slides](https://github.com/Helsinki-NLP/LLM-course-2024/blob/main/week-2/LLM-Course%20Lecture%202.pdf). | ||
|
||
|
||
## Preparation for the lab | ||
|
||
**Gemini API Key** | ||
* Use your existing google account or create a new free account | ||
* Go to https://aistudio.google.com/apikey | ||
* Get your API key and store it in a safe place | ||
|
||
**Create a HuggingFace account** | ||
* https://huggingface.co/join | ||
|
||
**Set up Python & Jupyter environment** | ||
* I use Python 3.12 for running code locally | ||
* For inference or fine-tuning requiring GPU, I use Google Colab: https://colab.research.google.com/ | ||
|
||
|
||
## Lab Exercise | ||
|
||
For this exercise use the gemini-chatbot and/or prompting-notebook found in GitHub under week-2 folder. | ||
Select a domain (e.g. finance, sports, cooking), tone of voice, style and persona (e.g. a pirate) and a question/task you want to accomplish (e.g. write a blog post) | ||
* Modify the gemini-chatbot and test the different prompting approaches discussed in the lecture to achieve the task. | ||
* Do the same for prompting-notebook (run this in Google Colab using a T4 GPU backend) | ||
* Write a section to your report explaining what you did and what were your findings. Which prompting approach worked the best and why? | ||
|
||
Modify the in-context-learning notebook (you can run this locally or in Google Colab) | ||
* Modify the prompt to change the style of the output to be a table with strengths and weaknesses in separate columns. (Markdown printing should show the table correctly. If you have time, modify the html printing to show the updated style as a table). | ||
* If you have time: modify the notebook to use an open source model from Hugging Face instead of Gemini | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from fasthtml.common import * | ||
import google.generativeai as genai | ||
import strip_markdown | ||
import configparser | ||
|
||
API_KEY = os.environ.get("GEMINI_API_KEY") | ||
genai.configure(api_key=API_KEY) | ||
LLM = "gemini-1.5-flash" | ||
model = genai.GenerativeModel(LLM) | ||
|
||
# Read system prompts from config file | ||
prompts = configparser.ConfigParser() | ||
prompts.read('prompts.env') | ||
|
||
# Set system prompt | ||
#system_prompt = prompts.get("SYSTEM_PROMPTS", "IT_HELPDESK") | ||
system_prompt = f'Summarize the following text about {prompts.get("TEMPLATES", "TOPIC")} in {prompts.get("TEMPLATES", "NUMBER")} bullet points:' | ||
|
||
# Set up the app, including daisyui and tailwind for the chat component | ||
hdrs = (picolink, Script(src="https://cdn.tailwindcss.com"), | ||
Link(rel="stylesheet", href="https://cdn.jsdelivr.net/npm/[email protected]/dist/full.min.css")) | ||
app = FastHTML(hdrs=hdrs, cls="p-4 max-w-lg mx-auto") | ||
|
||
# Chat message component (renders a chat bubble) | ||
def ChatMessage(msg, user): | ||
bubble_class = "chat-bubble-primary" if user else 'chat-bubble-secondary' | ||
chat_class = "chat-end" if user else 'chat-start' | ||
return Div(cls=f"chat {chat_class}")( | ||
Div('user' if user else 'assistant', cls="chat-header"), | ||
Div(msg, cls=f"chat-bubble {bubble_class}"), | ||
Hidden(msg, name="messages") | ||
) | ||
|
||
# The input field for the user message. | ||
def ChatInput(): | ||
return Input(name='msg', id='msg-input', placeholder="Type a message", | ||
cls="input input-bordered w-full", hx_swap_oob='true') | ||
|
||
# The main screen | ||
@app.get | ||
def index(): | ||
page = Form(hx_post=send, hx_target="#chatlist", hx_swap="beforeend")( | ||
Div(id="chatlist", cls="chat-box h-[73vh] overflow-y-auto"), | ||
Div(cls="flex space-x-2 mt-2")( | ||
Group(ChatInput(), Button("Send", cls="btn btn-primary")) | ||
) | ||
) | ||
return Titled('Simple chatbot demo', page) | ||
|
||
# Handle the form submission | ||
@app.post | ||
def send(msg:str, messages:list[str]=None): | ||
if not messages: messages = [system_prompt] | ||
messages.append(msg.rstrip()) | ||
r = model.generate_content(messages).text | ||
return (ChatMessage(msg, True), # The user's message | ||
ChatMessage(strip_markdown.strip_markdown(r.rstrip()), False), # The chatbot's response | ||
ChatInput()) # And clear the input field | ||
|
||
serve() | ||
|
Oops, something went wrong.