|
| 1 | +import json |
| 2 | +import os |
| 3 | +from typing import Dict |
| 4 | + |
| 5 | +from dotenv import load_dotenv |
| 6 | +from openai import OpenAI, BaseModel |
| 7 | + |
| 8 | +load_dotenv() |
| 9 | + |
| 10 | +deepseek_api_key = os.getenv("DEEP_SEEK_API_KEY") |
| 11 | +client = OpenAI(api_key=deepseek_api_key, base_url="https://api.deepseek.com") |
| 12 | +beta_seek_url = "https://api.deepseek.com/beta" |
| 13 | + |
| 14 | + |
| 15 | +# Coding / Math 0.0 |
| 16 | +# Data Cleaning / Data Analysis 1.0 |
| 17 | +# General Conversation 1.3 |
| 18 | +# Translation 1.3 |
| 19 | +# Creative Writing / Poetry 1.5 |
| 20 | + |
| 21 | +def get_all_models(): |
| 22 | + result = client.models.list() |
| 23 | + print(result) |
| 24 | + |
| 25 | + |
| 26 | +class BalanceInfo(BaseModel): |
| 27 | + currency: str |
| 28 | + total_balance: str |
| 29 | + granted_balance: str |
| 30 | + topped_up_balance: str |
| 31 | + |
| 32 | + |
| 33 | +def get_user_balance(): |
| 34 | + result = client.get("user/balance", cast_to=BalanceInfo) |
| 35 | + print(result) |
| 36 | + |
| 37 | + |
| 38 | +def code_completion(): |
| 39 | + response = client.completions.create( |
| 40 | + model="deepseek-chat", |
| 41 | + prompt="def fib(a):", |
| 42 | + suffix=" return fib(a-1) + fib(a-2)", |
| 43 | + max_tokens=128 |
| 44 | + ) |
| 45 | + print(response) |
| 46 | + |
| 47 | + |
| 48 | +def multiple_rounds_use_case(): |
| 49 | + # Round 1 |
| 50 | + messages = [{"role": "user", "content": "What's the highest mountain in the world?"}, |
| 51 | + # {"role": "assistant", "content": "```python\n", "prefix": True} beta |
| 52 | + ] |
| 53 | + response = client.chat.completions.create( |
| 54 | + model="deepseek-chat", |
| 55 | + messages=messages |
| 56 | + ) |
| 57 | + |
| 58 | + messages.append(response.choices[0].message) |
| 59 | + print(f"Messages Round 1: {messages}") |
| 60 | + |
| 61 | + # Round 2 |
| 62 | + messages.append({"role": "user", "content": "What is the second?"}) |
| 63 | + response = client.chat.completions.create( |
| 64 | + model="deepseek-chat", |
| 65 | + messages=messages |
| 66 | + ) |
| 67 | + |
| 68 | + messages.append(response.choices[0].message) |
| 69 | + print(f"Messages Round 2: {messages}") |
| 70 | + |
| 71 | + |
| 72 | +def json_output_use_case(): |
| 73 | + system_prompt = """ |
| 74 | + The user will provide some exam text. Please parse the "question" and "answer" and output them in JSON format. |
| 75 | +
|
| 76 | + EXAMPLE INPUT: |
| 77 | + Which is the highest mountain in the world? Mount Everest. |
| 78 | +
|
| 79 | + EXAMPLE JSON OUTPUT: |
| 80 | + { |
| 81 | + "question": "Which is the highest mountain in the world?", |
| 82 | + "answer": "Mount Everest" |
| 83 | + } |
| 84 | + """ |
| 85 | + |
| 86 | + user_prompt = "Which is the longest river in the world? The Nile River." |
| 87 | + |
| 88 | + messages = [{"role": "system", "content": system_prompt}, |
| 89 | + {"role": "user", "content": user_prompt}] |
| 90 | + |
| 91 | + response = client.chat.completions.create( |
| 92 | + model="deepseek-chat", |
| 93 | + messages=messages, |
| 94 | + response_format={ |
| 95 | + 'type': 'json_object' |
| 96 | + } |
| 97 | + ) |
| 98 | + |
| 99 | + print(json.loads(response.choices[0].message.content)) |
| 100 | + |
| 101 | + |
| 102 | +def send_messages(messages, tools): |
| 103 | + response = client.chat.completions.create( |
| 104 | + model="deepseek-chat", |
| 105 | + messages=messages, |
| 106 | + tools=tools |
| 107 | + ) |
| 108 | + return response.choices[0].message |
| 109 | + |
| 110 | + |
| 111 | +def function_call_use_case(): |
| 112 | + tools = [ |
| 113 | + { |
| 114 | + "type": "function", |
| 115 | + "function": { |
| 116 | + "name": "get_weather", |
| 117 | + "description": "Get weather of an location, the user should supply a location first", |
| 118 | + "parameters": { |
| 119 | + "type": "object", |
| 120 | + "properties": { |
| 121 | + "location": { |
| 122 | + "type": "string", |
| 123 | + "description": "The city and state, e.g. San Francisco, CA", |
| 124 | + } |
| 125 | + }, |
| 126 | + "required": ["location"] |
| 127 | + }, |
| 128 | + } |
| 129 | + }, |
| 130 | + ] |
| 131 | + |
| 132 | + messages = [{"role": "user", "content": "How's the weather in Hangzhou?"}] |
| 133 | + message = send_messages(messages, tools) |
| 134 | + print(f"User>\t {messages[0]['content']}") |
| 135 | + |
| 136 | + tool = message.tool_calls[0] |
| 137 | + messages.append(message) |
| 138 | + |
| 139 | + messages.append({"role": "tool", "tool_call_id": tool.id, "content": "24℃"}) |
| 140 | + message = send_messages(messages, tools) |
| 141 | + print(f"Model>\t {message.content}") |
| 142 | + |
| 143 | + |
| 144 | +if __name__ == '__main__': |
| 145 | + get_all_models() |
| 146 | + get_user_balance() |
| 147 | + # multiple_rounds_use_case() |
0 commit comments