Skip to content

Instantly share code, notes, and snippets.

View EnkrateiaLucca's full-sized avatar

LUCAS SOARES EnkrateiaLucca

View GitHub Profile
@EnkrateiaLucca
EnkrateiaLucca / function_calls_chatgpt.py
Created October 30, 2023 13:21
OpenAI function calling
# source: https://platform.openai.com/docs/guides/gpt/function-calling
import openai
import json
# Example dummy function hard coded to return the same weather
# In production, this could be your backend API or an external API
def get_current_weather(location, unit="fahrenheit"):
"""Get the current weather in a given location"""
weather_info = {
"location": location,
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@EnkrateiaLucca
EnkrateiaLucca / train_val_test_split.py
Created September 22, 2023 15:07
Splits dataset into train val and test
def get_dataset_partitions_pd(df, train_split=0.8, val_split=0.1, test_split=0.1, target_variable=None):
assert (train_split + test_split + val_split) == 1
# Only allows for equal validation and test splits
assert val_split == test_split
# Shuffle
df_sample = df.sample(frac=1, random_state=12)
@EnkrateiaLucca
EnkrateiaLucca / create_pdf.py
Created August 9, 2023 18:13
Creates a pdf using the fpdf package
import fpdf
def make_pdf(text):
"""Makes a PDF file from the given text."""
# Create a new FPDF object.
pdf = fpdf.FPDF()
# Add a page to the PDF object.
pdf.add_page()
@EnkrateiaLucca
EnkrateiaLucca / openai-chatgpt-call.py
Last active December 11, 2023 14:39
Call to chatgpt
from openai import OpenAI
client = OpenAI()
def get_response(prompt_question):
response = client.chat.completions.create(
model="gpt-3.5-turbo-16k",
messages=[{"role": "system", "content": "You are a helpful research and\
programming assistant"},
{"role": "user", "content": prompt_question}]
@EnkrateiaLucca
EnkrateiaLucca / tiktoken-calculate-tokens.py
Last active September 13, 2023 08:59
Calculates number of tokens
import tiktoken
def get_num_tokens(prompt, model="gpt-3.5-turbo"):
"""Calculates the number of tokens in a text prompt"""
enc = tiktoken.encoding_for_model("gpt-3.5-turbo")
return len(enc.encode(prompt))
from langchain.document_loaders import PyPDFLoader
loader = PyPDFLoader('./jung1.pdf')
pdf_doc = loader.load_and_split()
for page in pdf_doc:
print(page.page_content)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@EnkrateiaLucca
EnkrateiaLucca / testing_widgets.ipynb
Created July 28, 2023 12:02
a bunch of jupyter widgets
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@EnkrateiaLucca
EnkrateiaLucca / extract_from_urls.py
Created July 10, 2023 23:47
extracts text from urls
import re
text = """https://www.notion.so/enkrateialog/44a8f633dda74aa0b26b89ad960015fa?v=9be485bc35f44c68a9399f880844a45d | (7) Reference Sources
https://chat.openai.com/?model=gpt-4-plugins | ChatGPT
... (remaining text) ..."""
urls = re.findall(r'(https?://\S+)', text)
print(urls)