Skip to content

Commit 6aaa454

Browse files
committed
feat:Push all python examples
1 parent 9ebedbd commit 6aaa454

25 files changed

Lines changed: 512 additions & 24 deletions

‎ai-university-pre-course/dict.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
dictionary = {
2+
"Tennis": "Agassi",
3+
"Cricket": "Sachin",
4+
"Football": "Pele",
5+
"Cricket": "Kranthi"
6+
}
7+
print("dictionary:", dictionary.get("Cricket"))
8+
dictionary["Football"] = "Maradona"
9+
print("dictionary keys:", dictionary.values())
10+
11+
for dictElement in dictionary:
12+
print("dictElement::{}".format(dictElement))

‎ai-university-pre-course/list.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# A list is mutable.
2+
3+
4+
course = ['data science', 'machine learning', 'html', ['java', 'c++', 'Go']]
5+
#Slice
6+
print("Slice ::{}".format(course[:3]))
7+
8+
#All elements:
9+
print("All Elements ::{}".format(course))
10+
11+
complete_list = course[:4]
12+
print("Slice::{}".format(complete_list))
13+
14+
complete_list[2] = 'React'
15+
print("complete_list now::{}".format(complete_list))
16+
17+
print("2nd index element::{}".format(complete_list[2]))
18+
course.remove("html")
19+
20+
print("All Elements after removal::{}".format(course))
21+
22+
universities = ['UT', 'UIC', 'Western Illinois']
23+
24+
concatenate_lists = course + universities
25+
print("list concatenation::{}".format(concatenate_lists))
26+
27+
# Removing elements from list
28+
print("List after removed element::{}".format(concatenate_lists))
29+
30+
31+
# Pop element
32+
concatenate_lists.pop()
33+
print("List after pop::{}".format(concatenate_lists))
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
tuple_list = ('12', 'opposite', 100, ['blue', 'pink', 'white'], 1, 99)
2+
print("tuple_list:{}".format(tuple_list))
3+
4+
# 2nd Element
5+
print("2nd element:{}".format(tuple_list[2]))
6+
7+
# Change tuple value - THis is not possible as tupples are immutable
8+
9+
#tuple_list[1] = 'Reverse'
10+
11+
# Deleting an element in a tupple is not possible, however deleting complete tupple is possible
12+
print("tuple_list now:{}".format(tuple_list))
13+
14+
#del tuple_list[1] -> Not possible
15+
16+
#Sorting Elements of a tupple
17+
tupple_num = (100,7,88, 12, 91, -1)
18+
print("Sorted tupple_num:{}".format(sorted(tupple_num)))
19+
20+
#This can't be sorted as it contains numbers and strings
21+
#print("Sorted tupple:{}".format(sorted(tuple_list)))
22+
23+
# Other tupple functions
24+
25+
# count(), index(), sorted()

‎examples/ParserArgs.py‎

Lines changed: 0 additions & 17 deletions
This file was deleted.

‎examples/basics/data_types.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# When more than one operator appears in an expression, the order of evaluation
2+
# depends on the rules of precedence. For mathematical operators, Python follows
3+
# mathematical convention. The acronym PEMDAS is a useful way to remember
4+
# the rules:
5+
# PEMDAS -> Parenthesis, exponentation ( multiplication), M -> Multiplication D -> Division and A -> Addition , S -> Subtraction
6+
# Multiplication and Division have same precendence.
7+
8+
9+
# Data types are int, str, float
10+
11+
this_is_an_int = 12
12+
this_is_an_float = 123.45
13+
this_is_an_string = "Hello"
14+
print("{}{}{}{}{}{}".format(" 12 is", type(this_is_an_int), "\n 123.45 is::", type(this_is_an_float), "\n Hello is::",
15+
type(this_is_an_string)))
16+
17+
# Empty dictionary declaration
18+
empty_dict = dict()

‎examples/basics/floating_conversions.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
a = '35'
33
b = "45.26"
44
c = float(b) * float(a)
5-
print('{}{}'.format('c is ', c))
5+
print('{}{}'.format('c is ', c))
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import openai
2+
import os
3+
4+
from dotenv import load_dotenv, find_dotenv
5+
_ = load_dotenv(find_dotenv())
6+
7+
openai.api_key = os.getenv('OPENAI_API_KEY')
8+
9+
def get_completion(prompt, model="gpt-3.5-turbo"):
10+
messages = [{"role": "user", "content": prompt}]
11+
response = openai.ChatCompletion.create(
12+
model=model,
13+
messages=messages,
14+
temperature=0, # this is the degree of randomness of the model's output
15+
)
16+
return response.choices[0].message["content"]
17+
18+
19+
20+
prompt = f"""
21+
Generate a list of 5 famous Chicago basket players with their names, date of birth along with their team name \
22+
with their team id, player name and date of birth.
23+
Provide them in JSON format with the following keys:
24+
player_name, date_of_birth, team_name
25+
"""
26+
27+
response = get_completion(prompt)
28+
29+
print(response)

‎examples/chatgpt/davinci.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import os
2+
import openai
3+
4+
openai.api_key = os.getenv("OPENAI_API_KEY")
5+
print('openapi key:::', openai.api_key)
6+
7+
# Text-davinci-003
8+
9+
response = openai.Completion.create(
10+
model="text-davinci-003",
11+
# prompt="I am a highly intelligent question answering bot. If you ask me a question that is rooted in truth, I will give you the answer. If you ask me a question that is nonsense, trickery, or has no clear answer, I will respond with \"Unknown\".\n\nQ: What is human life expectancy in the United States?\nA: Human life expectancy in the United States is 78 years.\n\nQ: Who was president of the United States in 1955?\nA: Dwight D. Eisenhower was president of the United States in 1955.\n\nQ: Which party did he belong to?\nA: He belonged to the Republican Party.\n\nQ: What is the square root of banana?\nA: Unknown\n\nQ: How does a telescope work?\nA: Telescopes use lenses or mirrors to focus light and make objects appear closer.\n\nQ: Where were the 1992 Olympics held?\nA: The 1992 Olympics were held in Barcelona, Spain.\n\nQ: How many squigs are in a bonk?\nA: Unknown\n\nQ: Who is the president of United States ?\nA:",
12+
prompt="Q: Who is the president of India ?\nA:",
13+
temperature=0,
14+
max_tokens=20,
15+
top_p=1,
16+
frequency_penalty=0.0,
17+
presence_penalty=0.0,
18+
stop=["\n"]
19+
)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import os
2+
import openai
3+
openai.api_key = os.getenv("OPENAI_API_KEY")
4+
print('openapi key:::', openai.api_key)
5+
6+
response = openai.ChatCompletion.create(
7+
model="gpt-3.5-turbo",
8+
messages=[{"role": "user", "content": "Capital of U.S.A ?\n" }],#,
9+
#"content": "What is the Capital of U.S.A ?\n" }],op
10+
temperature=0,
11+
max_tokens=101,
12+
stop=["\n"]
13+
)
14+
15+
response1 = response['choices'][0]['message']
16+
17+
#$ openai api completions.create -m text-davinci-003 -p "Say this is a test" -t 0 -M 7 --stream
18+
19+
# response = openai.ChatCompletion.create(
20+
# model="gpt-3.5-turbo",
21+
# messages=[ # {"role": "system",
22+
# # "content": "You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible.\nKnowledge cutoff: 2021-09-01"},
23+
# # {"role": "user", "content": "How are you?"},
24+
# # {"role": "assistant", "content": "I am doing well"},
25+
# {"role": "user", "content": "What is the current GPT model ?\n",
26+
# "role": "user", "content": "What is the Capital of U.S.A ?"}],
27+
# temperature=0,
28+
# max_tokens=51,
29+
# top_p=1,
30+
# frequency_penalty=0.0,
31+
# presence_penalty=0.0,
32+
# stop=["\n"]
33+
# )
34+
print('response is::', response1)

‎examples/chatgpt/gpt-3.5.turbo.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import os
2+
import openai
3+
OPENAI_API_KEY='#################'
4+
5+
#openai.api_key = os.getenv("OPENAI_API_KEY")
6+
openai.api_key = OPENAI_API_KEY
7+
print('openapi key:::', openai.api_key)
8+
9+
#Instructions - Set the API Key in venv terminal as
10+
11+
response = openai.ChatCompletion.create(
12+
model="gpt-3.5-turbo",
13+
messages=[{"role": "user", "content": "Capital of India ?\n" }],
14+
temperature=0,
15+
max_tokens=101,
16+
stop=["\n"]
17+
)
18+
19+
response1 = response['choices'][0]['message']
20+
21+
print('response is::', response1)

0 commit comments

Comments
 (0)