Skip to content

Commit 51ad1d3

Browse files
committed
try Llama3-7b on Olama on the laptop
Signed-off-by: Jules Damji <[email protected]>
1 parent b823d46 commit 51ad1d3

7 files changed

+108
-22
lines changed

assistants/function_utils.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
warnings.filterwarnings('ignore')
1818
_ = load_dotenv(find_dotenv()) # read local .env file
1919

20-
openai.api_base = os.getenv("ANYSCALE_API_BASE", os.getenv("OPENAI_API_BASE"))
21-
openai.api_key = os.getenv("ANYSCALE_API_KEY", os.getenv("OPENAI_API_KEY"))
20+
# TODO: The 'openai.api_base' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(base_url=os.getenv("ANYSCALE_API_BASE", os.getenv("OPENAI_API_BASE")))'
21+
# openai.api_base = os.getenv("ANYSCALE_API_BASE", os.getenv("OPENAI_API_BASE"))
2222
weather_api_key = os.getenv("WEATHER_API_KEY")
2323
MODEL = os.getenv("MODEL")
2424
print(f"Using MODEL={MODEL}; base={openai.api_base}")
@@ -89,12 +89,12 @@ def create_dalle_image(params,
8989
size="1024x1024",
9090
quality=quality,
9191
n=1)
92-
92+
9393
return response.data[0].url
9494

9595
def get_weather_data(params:Dict[Any, Any]=None,
9696
api_base:str="http://api.weatherstack.com/current") -> Dict[str, str]:
97-
97+
9898
"""
9999
Retrieves weather data from the OpenWeatherMap API.
100100
"""
@@ -108,8 +108,8 @@ def get_weather_data(params:Dict[Any, Any]=None,
108108
warnings.filterwarnings('ignore')
109109
_ = load_dotenv(find_dotenv()) # read local .env file
110110

111-
openai.api_base = os.getenv("ANYSCALE_API_BASE", os.getenv("OPENAI_API_BASE"))
112-
openai.api_key = os.getenv("ANYSCALE_API_KEY", os.getenv("OPENAI_API_KEY"))
111+
# TODO: The 'openai.api_base' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(base_url=os.getenv("ANYSCALE_API_BASE", os.getenv("OPENAI_API_BASE")))'
112+
# openai.api_base = os.getenv("ANYSCALE_API_BASE", os.getenv("OPENAI_API_BASE"))
113113
weather_api_key = os.getenv("WEATHER_API_KEY")
114114
MODEL = os.getenv("MODEL")
115115
print(f"Using MODEL={MODEL}; base={openai.api_base}")
@@ -143,4 +143,4 @@ def get_weather_data(params:Dict[Any, Any]=None,
143143
print(f"Weather data for City: {params['query']}")
144144
print(f"Temperature : {weather_data['current']['temperature']}")
145145
print(f"Weather description : {weather_data['current']['weather_descriptions']}")
146-
146+

assistants/google_search_utils.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def google_search(params:Dict[Any,Any]) -> List[Dict[str, str]]:
1717
query = quote_plus(params["query"])
1818
api_key = params["api_key"]
1919
num_in_page = params["num_results"]
20-
20+
2121
url = f"https://api.apilayer.com/google_search?q={query}"
2222

2323
payload = {}
@@ -41,8 +41,8 @@ def google_search(params:Dict[Any,Any]) -> List[Dict[str, str]]:
4141
warnings.filterwarnings('ignore')
4242
_ = load_dotenv(find_dotenv()) # read local .env file
4343

44-
openai.api_base = os.getenv("ANYSCALE_API_BASE", os.getenv("OPENAI_API_BASE"))
45-
openai.api_key = os.getenv("ANYSCALE_API_KEY", os.getenv("OPENAI_API_KEY"))
44+
# TODO: The 'openai.api_base' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(base_url=os.getenv("ANYSCALE_API_BASE", os.getenv("OPENAI_API_BASE")))'
45+
# openai.api_base = os.getenv("ANYSCALE_API_BASE", os.getenv("OPENAI_API_BASE"))
4646
google_api_key = os.getenv("GOOGLE_API_KEY")
4747
MODEL = os.getenv("MODEL")
4848
print(f"Using MODEL={MODEL}; base={openai.api_base}")

fine-tuning/explore_hf_data.py

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import pandas as pd
2+
from datasets import load_dataset
3+
import uuid
4+
5+
"""
6+
Example code to explore the dataset from the HuggingFace Hub, derived
7+
from the Red Dot Design Award Product Description dataset and convert it into a pandas dataframe.
8+
Code is from the following source:
9+
https://huggingface.co/datasets/xiyuez/red-dot-design-award-product-description
10+
Databricks Book on GenAI. (2021). Fine-tuning a pre-trained model with the Hugging Face library.
11+
"""
12+
13+
if __name__ == "__main__":
14+
15+
#Load the dataset from the HuggingFace Hub
16+
rd_ds = load_dataset("xiyuez/red-dot-design-award-product-description")
17+
pd.set_option('display.max_columns', None)
18+
# convert the dataset into a pandas dataframe
19+
df = pd.DataFrame(rd_ds['train'])
20+
# print column names
21+
print(df.columns)
22+
print("------------------")
23+
24+
# print the first product, category, description, and text
25+
print(f"product:{df['product'][0]}")
26+
print(f"category:{df['category'][0]}")
27+
print(f"description:{df['description'][0]}")
28+
print("------------------")
29+
print(f"text: {df['text'][0]}")
30+
print("------------------")
31+
32+
# Combine the two attributes into an instruction string
33+
df['instruction'] = 'Create a detailed description for the following product: '+ df['product']+', belonging to category: '+ df['category']
34+
35+
df = df[['instruction', 'description']]
36+
# Get a 5000 sample subset for fine-tuning purposes
37+
df_sample = df.sample(n=5000, random_state=42)
38+
print(df_sample.columns)
39+
print("------------------")
40+
print(df_sample.head())
41+
print("------------------")
42+
43+
# Define template and format data into the template
44+
# for supervised fine-tuning
45+
template = """Below is an instruction that describes a task.
46+
Write a response that appropriately completes the
47+
request.
48+
### Instruction:
49+
{}
50+
### Response:\n"""
51+
52+
df_sample['prompt'] = df_sample["instruction"].apply(lambda x: template.format(x))
53+
df_sample.rename(columns={'description': 'response'}, inplace=True)
54+
df_sample['response'] = df_sample['response'] + "\n### End"
55+
df_sample = df_sample[['prompt', 'response']]
56+
# print df_sample column names
57+
print(f" Modified column names: {df_sample.columns}")
58+
print("------------------")
59+
print(df_sample.head(2))
60+
print("------------------")
61+
print(f"prompt:{df_sample['prompt'][0]}")
62+
print(f"response:{df_sample['response'][0]}")
63+
print("------------------")
64+
65+
# # test putting garbage in one of the original columns
66+
# # Create a copy of the original dataframe
67+
# df_copy = df.copy()
68+
# # Put garbage in the first row of the prompt column
69+
# # using a lambda function
70+
# print("before lambada ------------------")
71+
# print(df_copy.columns)
72+
# print("after lambada ------------------")
73+
# df_copy['prompt'] = df_copy['description'].apply(lambda x: len(x))
74+
# print(df_copy.columns)
75+
# print(df_copy.head(3))
76+
77+
78+
print(f"dataset-{uuid.uuid4()}")
79+

function-calling/openai_anyscale_parallel_function_calling_db.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def pretty_print_conversation(messages: List[dict]):
6767
"assistant": "blue",
6868
"function": "magenta",
6969
}
70-
70+
7171
for message in messages:
7272
if message["role"] == "system":
7373
print(colored(f"system: {message['content']}\n", role_to_color[message["role"]]))
@@ -83,8 +83,8 @@ def pretty_print_conversation(messages: List[dict]):
8383
if __name__ == "__main__":
8484
_ = load_dotenv(find_dotenv()) # read local .env file
8585
warnings.filterwarnings('ignore')
86-
openai.api_base = os.getenv("ANYSCALE_API_BASE", os.getenv("OPENAI_API_BASE"))
87-
openai.api_key = os.getenv("ANYSCALE_API_KEY", os.getenv("OPENAI_API_KEY"))
86+
# TODO: The 'openai.api_base' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(base_url=os.getenv("ANYSCALE_API_BASE", os.getenv("OPENAI_API_BASE")))'
87+
# openai.api_base = os.getenv("ANYSCALE_API_BASE", os.getenv("OPENAI_API_BASE"))
8888
MODEL = os.getenv("MODEL")
8989
print(f"Using MODEL={MODEL}; base={openai.api_base}")
9090

function-calling/openai_parallel_function_calling_external.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
def get_weather_data(params:Dict[Any, Any]=None,
1919
api_base:str="http://api.weatherstack.com/current") -> Dict[str, str]:
20-
20+
2121
"""
2222
Retrieves weather data from the OpenWeatherMap API.
2323
"""
@@ -51,7 +51,7 @@ def get_current_weather(location, units="f") -> Dict[str, str]:
5151
})
5252
print(weather_data)
5353
return weather_data
54-
54+
5555
def run_conversation(client: object, model: str) -> object:
5656
# Step 1: send the messages and available functions to the model
5757
messages = [{"role": "user", "content": "What's the weather like in three cities: San Francisco, Tokyo, and Paris?"}]
@@ -90,7 +90,7 @@ def run_conversation(client: object, model: str) -> object:
9090
print(f"tool_calls: {tool_calls}")
9191
# Step 2: check if the model wanted to call a function
9292
if tool_calls:
93-
93+
9494
# Step 3: call the function
9595
# Note: the JSON response may not always be valid; be sure to handle errors
9696
available_functions_table = {
@@ -125,12 +125,12 @@ def run_conversation(client: object, model: str) -> object:
125125
return second_response
126126
else:
127127
return response
128-
128+
129129
if __name__ == "__main__":
130130
_ = load_dotenv(find_dotenv()) # read local .env file
131131
warnings.filterwarnings('ignore')
132-
openai.api_base = os.getenv("ANYSCALE_API_BASE", os.getenv("OPENAI_API_BASE"))
133-
openai.api_key = os.getenv("ANYSCALE_API_KEY", os.getenv("OPENAI_API_KEY"))
132+
# TODO: The 'openai.api_base' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(base_url=os.getenv("ANYSCALE_API_BASE", os.getenv("OPENAI_API_BASE")))'
133+
# openai.api_base = os.getenv("ANYSCALE_API_BASE", os.getenv("OPENAI_API_BASE"))
134134
weather_api_key = os.getenv("WEATHER_API_KEY")
135135
MODEL = os.getenv("MODEL")
136136
print(f"Using MODEL={MODEL}; base={openai.api_base}")

function-calling/streamlit_func_calling_db_app.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def chat_completion_request(clnt:object, messages:object,
5858
print("Unable to generate ChatCompletion response")
5959
print(f"Exception: {e}")
6060
return e
61-
61+
6262
conn = connect_db("customers.db")
6363
database_schema_string = get_database_schema(conn)
6464

@@ -153,8 +153,8 @@ def get_answer(client: object,
153153

154154
_ = load_dotenv(find_dotenv()) # read local .env file
155155
warnings.filterwarnings('ignore')
156-
openai.api_base = os.getenv("ANYSCALE_API_BASE", os.getenv("OPENAI_API_BASE"))
157-
openai.api_key = os.getenv("ANYSCALE_API_KEY", os.getenv("OPENAI_API_KEY"))
156+
# TODO: The 'openai.api_base' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(base_url=os.getenv("ANYSCALE_API_BASE", os.getenv("OPENAI_API_BASE")))'
157+
# openai.api_base = os.getenv("ANYSCALE_API_BASE", os.getenv("OPENAI_API_BASE"))
158158
MODEL = os.getenv("MODEL")
159159
print(f"Using MODEL={MODEL}; base={openai.api_base}")
160160

llm-prompts/basic_ollama_llama3.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import ollama
2+
3+
response = ollama.generate(model="llama3", prompt="Why is the sky blue?", stream=True)
4+
# Stream response
5+
for chunk in response:
6+
data = chunk["response"]
7+
print(data, end="")

0 commit comments

Comments
 (0)