Skip to content

Instantly share code, notes, and snippets.

View CivilEngineerUK's full-sized avatar

Michael Rustell CivilEngineerUK

View GitHub Profile
@CivilEngineerUK
CivilEngineerUK / serialize_openai_output.py
Created December 5, 2023 09:38
Example of serializing OpenAI completions so that it can be transmitted easily via REST
"""
# OpenAI Chat Completions Serialization Example
## Description
This code snippet demonstrates how to serialize the response from the OpenAI Chat Completions API into a JSON format which is useful for sending using REST.
It includes a helper function, `serialize_completion`, that takes the completion response object and converts it into a JSON-compatible dictionary.
## Usage
To use this code, you will need to have a .env file in the same directory as this script with your openai key:
OPENAI_API_KEY = "sk-your_key_here"
@CivilEngineerUK
CivilEngineerUK / DirectoryLoaderUnstructuredAPI.py
Created December 2, 2023 14:13
Uses Unstructured API to load directory into Langchain Document class. Can be used as a direct substitute for DirectoryLoader
"""
Title: Directory Loader using Unstructured API with Error Handling and LangChain Document Output
Description: A class for loading documents from a directory using the LangChain format.
It supports recursive loading and conversion of documents to the LangChain Document class using the UnstructuredClient API.
The class includes built-in error handling and outputs documents in the same style as the LangChain Document class.
To use, get a free unstructured API key here: https://unstructured.io/api-key
Author: @CivilEngineerUK
from pydantic import BaseModel, Field
from typing import Optional, List
from instructor import OpenAISchema
import json
class Variable(BaseModel):
name: str = Field(..., description="Name of the variable in markdown math")
description: str = Field(..., description="Description of the variable")
unit: Optional[str] = Field(None, description="Unit of the variable")
@CivilEngineerUK
CivilEngineerUK / async_openai_function_call
Last active November 16, 2023 08:09
Async openai api function call
"""
Title: Asynchronous OpenAI API Wrapper with Error Handling
Description: A Python script to interact with the OpenAI API asynchronously, with built-in error handling and retry logic.
This script is designed to handle multiple prompts with the ability to define custom functions for more complex queries.
Created from guidance at: https://platform.openai.com/docs/guides/gpt/function-calling
Updated: rev 1 - 16/11/2023 for new end point and gpt-4-1106-preview [openai==1.2.4]
- includes structural engineering beam formula schema and use of Pydantic and instructor
@CivilEngineerUK
CivilEngineerUK / R_interact_Excel.R
Last active April 30, 2019 11:00
Programmatically interact with an Excel spreadsheet from R, allowing inputs to be modified and results from dependent cells exported. Useful for sensitivity analyses using existing Excel spreadsheet calculations
#' R_interact_Excel
#'
#' Allows a spreadsheet to be modified programatically and the
#' output from any number of dependent cells returned as a \code{tibble}.
#' Uses the \code{RDCOMClient}, \code{dplyr} and \code{stringr} packages
#'
#' @param file_location file location of the spreadsheet to modify. The
#' spreadsheet should be \code{.xls} format
#' @param input_values a vector of values corresponding to the cells to be modified
#' @param input_cells a character vector of the input cells